Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create different seeds for different instances of "Random"

Tags:

c#

.net

random

People usually ask why they get always the same numbers when they use Random. In their case, they unintenionally create a new instance of Random each time (instead of using only one instance), which of course leads to the same numbers the whole time. But in my case, I do need several instances of Random which return different number streams.

Using hard-coded seeds is a bad idea in my opinion since you get the same values again and again after restarting the program. What about this:

int seed1 = (int)DateTime.Now.Ticks - 13489565;
int seed2 = (int)DateTime.Now.Ticks - 5564;

I know this looks silly and naive, but it would avoid the same values after every restart and both of the seeds should differ. Or maybe

Random helper = new Random();
int seed1 = helper.Next(1, int.MaxValue);
int seed2 = helper.Next(1, int.MaxValue);

As you can see, I am a bit uncreative here and need your help. Thanks.

like image 394
ceran Avatar asked Dec 21 '12 22:12

ceran


People also ask

How are random seeds generated?

Random seeds are often generated from the state of the computer system (such as the time), a cryptographically secure pseudorandom number generator or from a hardware random number generator.

How do you pick a seed for a random number generator?

Some people use an easy-to-remember sequence such as their phone number or the first few digits of pi. Others use long prime numbers such as 937162211. Still others bang like crazy on their keyboard, hoping that the resulting seed will be "random enough."

How do you generate a random seed in Python?

To generate random number in Python, randint() function is used. This function is defined in random module.

How do you create a random seed in Java?

There are two ways we can generate random number using seed. Random random = new Random(long seed); Random random1 = new Random(); random1. setSeed(seed); The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).


3 Answers

Jon Skeet, suggests using a secondary Random object and a lock to create a Random object factory. For example:

    public static Random NewRandom() 
    { 
        lock (globalLock) 
        { 
            return new Random(secondaryRandom.Next()); 
        } 
    } 

Take a look at the complete source code in Jon Skeet's blog.

like image 178
Diego Avatar answered Oct 06 '22 01:10

Diego


I would use your second approach:

int randomCount = 10;
Random seeder = new Random();
var randoms = Enumerable.Range(0, randomCount)
    .Select(i => new Random(seeder.Next()))
    .ToList();

This uses linq to create a list of 10 differently-seeded Random instances. You'll therefore need using System.Linq; to have access to the Select extension method.

You could also reuse the seeder as one of your instances:

int randomCount = 10;
Random seeder = new Random();
var randoms = Enumerable.Range(0, randomCount - 1)
    .Select(i => new Random(seeder.Next()))
    .Concat(new [] { seeder })
    .ToList();
like image 39
phoog Avatar answered Oct 06 '22 02:10

phoog


You could use a cryptographic generator to create your seeds e.g.

public static Random CreateRandom()
{
    using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
    {
        byte[] bytes = new byte[4];
        rng.GetNonZeroBytes(bytes);
        int seed = BitConverter.ToInt32(bytes, 0);
        return new Random(seed);
    }
}

Of course, if you just need ints then you could use RNGCryptoServiceProvider to generate them directly, although System.Random is probably faster.

like image 44
Lee Avatar answered Oct 06 '22 03:10

Lee