473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 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
8 3887
"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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
5
by: Bill | last post by:
Hello, If I type the following code directly into the interpreter, it works. If I run it from a script, it generates the following error. Can someone help? Thanks! ------------------------ ...
10
by: Marshall Belew | last post by:
I'm trying to synchronize a network app that uses random numbers generated by System.Random. Rather than pass every randomly generated number, I just pass the seed. I'm seeing a result that leads...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
2
by: Brendon Towle | last post by:
I need to simulate scenarios like the following: "You have a deck of 3 orange cards, 5 yellow cards, and 2 blue cards. You draw a card, replace it, and repeat N times." So, I wrote the...
6
by: Pao | last post by:
My code works in this way: I declared a static array in a class (public static int GVetRandom = new int;) that in a for cycle I fill with random numbers. The array gets cleared (clear method) and...
6
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
15
by: caca | last post by:
Hello, This is a question for the best method (in terms of performance only) to choose a random element from a list among those that satisfy a certain property. This is the setting: I need to...
4
by: globalrev | last post by:
do i need to import something to use random?
11
by: Alex | last post by:
Hi everybody, I wonder if it is possible in python to produce random numbers according to a user defined distribution? Unfortunately the random module does not contain the distribution I need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.