C Sharp - Generate Random Numbers for N number of digits.

Generate random numbers depending on number of digits entered.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NumberGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            //Accept the numbers here and then press Enter.
            Console.Write("Enter the digits in Housie/Thambola : ");
            int digits = Convert.ToInt32(Console.ReadLine());
            //This will call a RandomGenerator Class which will give a random number between 1 and 99.
            RamdomGenerator rg = new RamdomGenerator();
            //again label to use the code.
            again :
            for (int i = 0; i < digits; i++)
            {
                Console.WriteLine(rg.randomGen());
                Console.ReadKey();
            }

            Console.WriteLine("These are the lucky numbers. Thanks. If you want to play again, please press 1");
            //This is to read the key that is being typed in.
            ConsoleKeyInfo info = Console.ReadKey();
            if (info.Key == ConsoleKey.Enter)
            {
                    Console.WriteLine("Thanks for playing...");
                    Console.ReadLine();
                    return;
            }
            if (info.Key == ConsoleKey.D1) //D1 is equivalent to press number 1
            {
                Console.WriteLine();
                goto again;
            }

            //The code below can handle when you press 1, but if you press Enter it throws an exception
            //Hence use the above written code for key input.
            //if(Convert.ToInt32(Console.ReadLine()) != 1)
            //{
            //    Console.WriteLine("Thanks for playing...");
            //    Console.ReadLine();
            //    return;
            //}
            //else
            //{
            //    goto again;
            //}
        }
    }
    class RamdomGenerator
    {
        public int randomGen()
        {
            int temp;
            Random r = new Random();
            temp = r.Next(1, 100);
            return (temp);
        }       
    }
}

Output :

No comments: