471,594 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

How random is Random()

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
3 4493
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
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
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.

Similar topics

28 posts views Thread by Paul Rubin | last post: by
6 posts views Thread by Acacia | last post: by
10 posts views Thread by Virus | last post: by
10 posts views Thread by Johnny Snead | last post: by
10 posts views Thread by Marshall Belew | last post: by
15 posts views Thread by Steven Macintyre | last post: by
4 posts views Thread by tshad | last post: by
8 posts views Thread by Daniel | last post: by
6 posts views Thread by Mike Langworthy | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.