Alex Rivera | Logout

How do I seed a random class to avoid getting duplicate random values

Asked 2009-11-23T20:31:58.153
160

I have the following code inside a static method in a static class:

Random r = new Random();
int randomNumber = r.Next(1,100);

I have this inside a loop and I keep getting the same randomNumber!

Any suggestions here?

Edit
Report

2 Answers

5
public static Random rand = new Random(); // this happens once, and will be great at preventing duplicates

Note, this is not to be used for cryptographic purposes.

answered 2009-11-23T20:33:56.387
0

this workes for me:

private int GetaRandom()
    {
        Thread.Sleep(1);
        return new Random(DateTime.Now.Millisecond).Next();
    }
answered 2012-09-27T17:10:15.850

Your Answer