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.
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.
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."
To generate random number in Python, randint() function is used. This function is defined in random module.
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).
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.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With