By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 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+
8 Replies


P: n/a
"Daniel" <Da*****@vestryonline.comwrote in
news:uO**************@TK2MSFTNGP03.phx.gbl:
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

Try looking into crypto stuff as they usually have much better random
number generation then standard windows/.net/etc.
Jan 15 '07 #2

P: n/a
Hi,
Try to find something random you can use to generate the seed!

Examples include

i) for a web server the sum of the last 100 unique IPs to access the
server
ii) get a user to move their mouse randomly and use some of the
captured points

etc

DANGEROUS COMMENT: as long as no one knows your seed, you should be
ok...
HTH,
James.

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?

Thanks
Jan 15 '07 #3

P: n/a
Thanks for replies,

Isn't that what i ddi, used a random seed? My first random number generates
a seed value from a random number seeded by elapsed ticks since server
started.....then i use that randomseed to generate a random number between 0
and 99999999 to seed the next random number and then i tell that to find a
random number between my variables?

Why isn't that a random enough seeding strategy?

"pigeonrandle" <pi**********@hotmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Hi,
Try to find something random you can use to generate the seed!

Examples include

i) for a web server the sum of the last 100 unique IPs to access the
server
ii) get a user to move their mouse randomly and use some of the
captured points

etc

DANGEROUS COMMENT: as long as no one knows your seed, you should be
ok...
HTH,
James.

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?

Thanks

Jan 15 '07 #4

P: n/a
Hi,
In fairness, it should be ... but you said that the same numbers kept
coming up :0).

What range of numbers are you looking for?

James.

Daniel wrote:
Thanks for replies,

Isn't that what i ddi, used a random seed? My first random number generates
a seed value from a random number seeded by elapsed ticks since server
started.....then i use that randomseed to generate a random number between 0
and 99999999 to seed the next random number and then i tell that to find a
random number between my variables?

Why isn't that a random enough seeding strategy?

"pigeonrandle" <pi**********@hotmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Hi,
Try to find something random you can use to generate the seed!

Examples include

i) for a web server the sum of the last 100 unique IPs to access the
server
ii) get a user to move their mouse randomly and use some of the
captured points

etc

DANGEROUS COMMENT: as long as no one knows your seed, you should be
ok...
HTH,
James.

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?

Thanks
Jan 15 '07 #5

P: n/a
On Mon, 15 Jan 2007 13:14:07 -0000, "Daniel"
<Da*****@vestryonline.comwrote:
>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
You appear to have at least three PRNGs going, randomSeedm _random and
random, though the last might be a missing underscore. There is
nothing wrong with the principle of using randomSeed to generate seeds
for a set of further PRNGs, though it might be easier to just rely on
one PRNG for everything.

Without seeing the rest of your code I cannot tell why you are getting
the same set of numbers. One possibility is if min and max are too
close together then only a limited range of numbers can be returned.

If you are not happy with the standard C# PRNG, then there is the
cryptographic version:
System.Security.Cryptography.RandomNumberGenerator or
System.Security.Cryptography.RNGCryptoServiceProvi der which both point
back to the CryptAPI RNG.

RFC 4086 describes the CryptAPI RNG thus:
"The Windows CryptAPI cryptographic service provider stores a seed
state variable with every user. When CryptGenRandom is called,
this is combined with any randomness provided in the call and with
various system and user data such as the process ID, thread ID,
system clock, system time, system counter, memory status, free disk
clusters, and hashed user environment block. This data is all fed
to SHA-1, and the output is used to seed an RC4 key stream. That
key stream is used to produce the pseudo-random data requested and
to update the user's seed state variable.

"Users of Windows ".NET" will probably find it easier to use the
RNGCryptoServiceProvider.GetBytes method interface."

Note that this is a cryptographic RNG - there is no user definable
seed so you cannot generate the same set of random numbers twice.
SHA-1 is a cryptographic hash function and RC4 is a stream cypher,
which is in effect a very long period PRNG. Both are well covered in
Wikipedia if you are interested.

Alternatively you might want to write your own PRNG, then Chapter
Seven of "Numerical Recipes on C" is very helpful:
http://www.nrbook.com/a/bookcpdf.php

The code is in C and an example of the saying "Real programmers can
write Fortran in any language", but it is still a useful basis to work
from.

Knuth Volume Two is also useful for non-cryptographic PRNG background.

rossum

Jan 15 '07 #6

P: n/a
Hi Daniel,

the seed of your second random is directly dependent on the seed of the
first random. So, if you run this code several times with the same
ElapsedTicks, all the randoms will return the same sequence.
The parameterless constructor of random already generates a time dependent
seed, so that should do the same.Though I don't know the resolution it uses.

"Daniel" <Da*****@vestryonline.comschrieb im Newsbeitrag
news:OZ**************@TK2MSFTNGP02.phx.gbl...
Thanks for replies,

Isn't that what i ddi, used a random seed? My first random number
generates a seed value from a random number seeded by elapsed ticks since
server started.....then i use that randomseed to generate a random number
between 0 and 99999999 to seed the next random number and then i tell that
to find a random number between my variables?

Why isn't that a random enough seeding strategy?

"pigeonrandle" <pi**********@hotmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
>Hi,
Try to find something random you can use to generate the seed!

Examples include

i) for a web server the sum of the last 100 unique IPs to access the
server
ii) get a user to move their mouse randomly and use some of the
captured points

etc

DANGEROUS COMMENT: as long as no one knows your seed, you should be
ok...
HTH,
James.

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?

Thanks


Jan 15 '07 #7

P: n/a
Take a look at http://www.random.org/. I am pretty sure that they have a web
service which returns really random numbers.
Ethan
"Daniel" <Da*****@vestryonline.comwrote in message
news:uO**************@TK2MSFTNGP03.phx.gbl...
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 #8

P: n/a
After more searching i discovered i was seeding my numbers inside the method
that called them. I am not getting the same numbers over and over just
sometime si get a sequence repeated. Apparently if your range is small and
you call it often its possible i can seed with the same number twice.

Thanks for the ideas, especially the crypto method i will investigate that
further.

Thanks to all the replies
"Ethan Strauss" <ethan dot strauss at Promega dot comwrote in message
news:ON*************@TK2MSFTNGP04.phx.gbl...
Take a look at http://www.random.org/. I am pretty sure that they have a
web service which returns really random numbers.
Ethan
"Daniel" <Da*****@vestryonline.comwrote in message
news:uO**************@TK2MSFTNGP03.phx.gbl...
>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 #9

This discussion thread is closed

Replies have been disabled for this discussion.