Click here to Skip to main content
15,884,975 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have written the following code to print random numbers on the console.
C#
class Program
    {
        
        static void Main(string[] args)
        {
            
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());


            Console.ReadLine();
        }

        private static int randomNo()
        {
            Random r = new Random();
            int n = r.Next(56);
            return n;
        }


The moment i compile this code, it actually repeating the same numbers . I thought that it will generate random numbers like (4,5,6,9,8,9,6) but its not. I looked over several resources on internet and came to know that
C#
Random r = new Random()

must be defined on the class level and it worked well. But i am not able to figure out why is it needed to define at class level?

And what are Seed?

Please help me out to get this logic.
Posted

try this

C#
class Program
{
    static void Main(string[] args)
    {

        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());
        Console.WriteLine(randomNo());


        Console.ReadLine();
    }
   static Random r = new Random();  // declare it outside..
    private static int randomNo()
    {
        int n = r.Next(56);
        return n;
    }
}


Check these
Random[^]
c# Random[^]
 
Share this answer
 
Comments
Member 10524556 5-Feb-14 4:37am    
My one question left, Why is it needed to put Random variable definition at Class level? Please help me to explain it.
Karthik_Mahalingam 5-Feb-14 4:39am    
then only it will gives you a new random number in the r.Next(x) value.
Next means -> next random value of the current instance of the object. so object shld be common til the end.
else you will get the same random value again and again..
The random number generation starts from a seed value. If the same seed is used repeatedly, the same series of numbers is generated. One way to produce different sequences is to make the seed value time-dependent, thereby producing a different series with each new instance of Random. By default, the parameterless constructor of the Random class uses the system clock to generate its seed value, while its parameterized constructor can take an Int32 value based on the number of ticks in the current time. However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers.

Seed is a number used to calculate a starting value for the pseudo-random number sequence. If a negative number is specified, the absolute value of the number is used.


C#
using System;
using System.Data.SqlClient;

namespace Test
{
    class Program
    {
       private static Random r = new Random(25);
        static void Main(string[] args)
        {

            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());
            Console.WriteLine(randomNo());


            Console.ReadLine();
        }

        private static int randomNo()
        {
            int n = r.Next(56);
            return n;
        }
    }


}
 
Share this answer
 
v2
Comments
Member 10524556 5-Feb-14 4:36am    
My one question left is Why is it needed to put Random variable definition at Class level?
Sandeep Singh Shekhawat 5-Feb-14 4:48am    
I defined it class level because you can get next value of sequence from seed. Suppose you define it in a function then each function call it creates a new instance and it uses same seed value for each instance so your series will be same. but you want to generate random number sequence then I create it class level so each time you use first created instance and then get new value. If you can create a parameteries random number that deppends on time then you can create inside function after than you can also get a different series of number.

in sort each independent random number instance gives same random number when it creates each time.
try this link for more information about the random Number Generate

http://csharpindepth.com/Articles/Chapter12/Random.aspx[^]

and another link is.
http://stackoverflow.com/questions/13539974/random-number-generator-c-sharp[^]
 
Share this answer
 
A random seed (or seed state, or just seed) is a number (or vector) used to initialize a pseudorandom number generator.

Thus you need a new seed to start off a series of random numbers - else you will continuously get the same set of numbers.
To seed, you could try
new Random(new System.DateTime().Millisecond).Next();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900