By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

How random is Random()

P: n/a
Hey guys

Using Random(), how random is it, is it possible to be predicted? I have a
requirement for a very good random number generator. I was doing something
such as:

Random randomSeed = new Random((int)_seedTimer.ElapsedTicks);
_random = new Random(randomSeed.Next(0,99999999));

return random.Next(min, max);

But i am finding that in some cases the same sets of numbers appear or
regularly occur? any idea why? Any tips on better methods for truly random
numbers?

Thanks
Jan 15 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Well, how about let it worry about the (time-based) seed using the
default ctor? Note that in either strategy Random instances created
within a similar time range will get the same seed due to the inherent
accuracy of the system timer (not very); your best approach when
generating lots of random numbers is to keep re-using an instance
(within a thread or allowing for locking) rather than creating lots of
Randoms...

Marc/
Jan 15 '07 #2

P: n/a
Or as an alternative, how about something along the lines of below;
allows (thread-sync'd) access to a shard instance for simple usage, or
a Create method for creating standalone Ranom instances, but with a
seed based on the overall (static) random - i.e. calling .Create()
repeatedly within microseconds will never [singing: well hardly ever!]
return the same seeds.

Marc

static class RandomUtil
{
private readonly static Random rand = new Random();
static int Next()
{
lock (rand)
{
return rand.Next();
}
}
static Random Create()
{
return new Random(Next());
}
}
Jan 15 '07 #3

P: n/a
Daniel wrote:
Hey guys

Using Random(), how random is it, is it possible to be predicted? I
have a requirement for a very good random number generator. I was
doing something such as:

Random randomSeed = new
Random((int)_seedTimer.ElapsedTicks); _random = new
Random(randomSeed.Next(0,99999999));
return random.Next(min, max);

But i am finding that in some cases the same sets of numbers appear or
regularly occur? any idea why? Any tips on better methods for truly
random numbers?
As others have replied, Random instances with time corellated seeds will
generate time corellated values. The Random class is a simple linear
congruential generator [1]. If you need random numbers for a security
purpose (e.g. encryption keys), then you need to use a cryptographically
strong random number generator. One such strong random number generator is
exposed by the System.Security.Cryptography.RNGCryptoServiceProvi der class
[2].

-cd

[1] http://en.wikipedia.org/wiki/Linear_...ial_Generators
[2]
http://msdn2.microsoft.com/en-us/lib...eprovider.aspx


Jan 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.