views:

512

answers:

5

How to generate random int number? (C#)

+7  A: 
Random r = new Random();
int n = r.Next();
Joren
+3  A: 

new Random().Next( int.MinValue, int.MaxValue )

For more information, look at the Random class, though please note:

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

Fyodor Soikin
No need to seed the random - it does that on creation.
Callum Rogers
Indeed, it does. Thank you. Corrected the post.
Fyodor Soikin
-1: The default seed is based on time; do this in a loop and you'll get very non-random results. You should create **one** generator and use it for all your numbers, not a separate generator each time.
Bevan
Hey, that's unfair. The question was how to generate random int number. No loops or series were mentioned.
Fyodor Soikin
Ok, fair point. Rescinded. Though, I still think that not using `new Random()` in a loop is an important point.
Bevan
Sorry, couldn't change my vote unless the post was edited ... adding the quote seemed the fairest thing to do.
Bevan
@Bevan: Thank you :-)
Fyodor Soikin
+6  A: 

The Random class is used to create random numbers. (Pseudo-random that is of course.)

Example:

Random rnd = new Random();
int month = rnd.Next(1, 13); // creates a number between 1 and 12
int dice = rnd.Next(1, 7); // creates a number between 1 and 6
int card = rnd.Next(52); // creates a number between 0 and 51

If you are going to create more than one random number, you should keep the Random instance and reuse it. If you create new instances too close in time, they will produce the same series of random numbers as the random generator is seeded from the system clock.

Guffa
A: 

You could use Jon Skeet's StaticRandom method inside the MiscUtil class library that he built for a truly random number.

using System;
using MiscUtil;

class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i < 100; i++)
        {
            Console.WriteLine(StaticRandom.Next());
        }
    }
}
mbcrump
"truly random"? How does that work?
Andreas Rejbrand
I just had a look at the source code, and this function uses exactly the same random number engine, the one "included" in C#, but makes sure that the same "seed"/"mother object" is used for all calls. (I am sorry that I do not know the C# terminology. But my point is that this function does not make any better random numbers than the standard function.)
Andreas Rejbrand
+12  A: 

Every time you do new Random() it is initialized . This means that in a tight loop you get the same value lots of times. You should keep a single Random instance and keep using Next on the same instance.

//Function to get random number
private static readonly Random getrandom = new Random();
private static readonly object syncLock = new object();
public static int GetRandomNumber(int min, int max)
{
    lock(syncLock) { // synchronize
        return getrandom .Next(min, max);
    }
}
Pankaj Mishra
Is this not what @Guffa said in his answer 6 months ago? "If you create new instances too close in time, they will produce the same series of random numbers"
Chris
@Chris- That's right what you said. In this I have provided the implementation of that. I think it's a good way of doing it. It works better.
Pankaj Mishra