473,221 Members | 1,936 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,221 software developers and data experts.

Random number generator and seeding

Joe
Hi, I have been working on some code that requires a high use of random
numbers within. Mostly I either have to either:
1) flip a coin i.e. 0 or 1, or
2) generate a double between 0 and 1.

I have utilised the following random number source code
http://www.agner.org/random/

What I have found is that there is a problem with seeding. The code
generates a seed based on time(0). I have found that I need to increment
seed and

A snippet of my code is below. I basically generate a Population object that
contains various individuals. I want to randomly choose two individuals from
this population. I wish I could choose these individuals by the following:

// get two parents
ind1 = pool.RouletteWheel();
ind2 = pool.RouletteWheel();
Jul 22 '05 #1
3 7323

"Joe" <jo*@blah.com> wrote in message news:41******@dnews.tpgi.com.au...
Hi, I have been working on some code that requires a high use of random
numbers within. Mostly I either have to either:
1) flip a coin i.e. 0 or 1, or
2) generate a double between 0 and 1.

I have utilised the following random number source code
http://www.agner.org/random/

What I have found is that there is a problem with seeding. The code
generates a seed based on time(0). I have found that I need to increment
seed and

A snippet of my code is below. I basically generate a Population object
that contains various individuals. I want to randomly choose two
individuals from this population. I wish I could choose these individuals
by the following:

// get two parents
ind1 = pool.RouletteWheel();
ind2 = pool.RouletteWheel();
.
.
int Population :: RouletteWheel ( )
{
int seed = time(0);
TRandomMersenne rg(seed);
return rg.Random();
}

In the above case all the random functions are contained in the Population
class. But when i do this it passes back two identical individuals. So
nstead I had to implement passing an incremented seed to the RouletteWheel
member function as shown below
<snip>
I dont like this solution as it enforced defining random functions in the
main.cpp class - which i dont want. Is there any way to implement my
desired approach (the former). I dont like the idea of passing incremented
seeds .. even though it does appear to introduce randomness in my results.

Any ideas greatly appreciated!

Cheers,
Joe


You only want to seed a random-number generator once during a given run of a
program. You then call the RNG multiple times to get "random" values.
Seeding the RNG multiple times destroys the "randomness" of the RNG.

-Howard


Jul 22 '05 #2

"Joe" <jo*@blah.com> schrieb im Newsbeitrag
news:41******@dnews.tpgi.com.au...
Hi, I have been working on some code that requires a high use of random
numbers within. Mostly I either have to either:
1) flip a coin i.e. 0 or 1, or
2) generate a double between 0 and 1.

I have utilised the following random number source code
http://www.agner.org/random/

What I have found is that there is a problem with seeding. The code
generates a seed based on time(0). I have found that I need to increment
seed and

A snippet of my code is below. I basically generate a Population object that contains various individuals. I want to randomly choose two individuals from this population. I wish I could choose these individuals by the following:

[SNIP]

I guess it's necessary to clarify some things about random number
generators. In principle they work the following way that you have to pass a
starting seed which is the starting point of your sequence of (pseudo)random
numbers. Passing the same seed to the RNG will result in the same sequence.
This is useful if you need random numbers but simultaneously have the
requirement of reproducing the behavior of your algorithm.

However, normally the seed is passed only at the construction time and
subsequent calls to the RNG's function which actually returns the numbers,
should modify the internally stored seed of the RNG object. You should be
aware that seeds are not numbers which will continuously increase or
decrease. Furthermore, the seed has a great influence on the quality of the
random sequence but this would lead too far here.

In summary you should pass the seed only once to your random number object
and also this object should be created only once.

int Population :: RouletteWheel ( int seed )
{
TRandomMersenne rg(seed);
return rg.Random();
}

This implementation creates a RNG object whenever RouletteWheel is called. I
don't know about the details of your population class but you could put the
TRandomMersenne object there as a member which is initialized with the seed
in the constructor of "population".

HTH
Chris
Jul 22 '05 #3
Chris Theis wrote:

"Joe" <jo*@blah.com> schrieb im Newsbeitrag
news:41******@dnews.tpgi.com.au...
Hi, I have been working on some code that requires a high use of random
numbers within. Mostly I either have to either:
1) flip a coin i.e. 0 or 1, or
2) generate a double between 0 and 1.

I have utilised the following random number source code
http://www.agner.org/random/

What I have found is that there is a problem with seeding. The code
generates a seed based on time(0). I have found that I need to increment
seed and

A snippet of my code is below. I basically generate a Population object

that
contains various individuals. I want to randomly choose two individuals

from
this population. I wish I could choose these individuals by the following:

[SNIP]

I guess it's necessary to clarify some things about random number
generators. In principle they work the following way that you have to pass a
starting seed which is the starting point of your sequence of (pseudo)random
numbers. Passing the same seed to the RNG will result in the same sequence.
This is useful if you need random numbers but simultaneously have the
requirement of reproducing the behavior of your algorithm.


Good explanation.
But I want to express something clearly to the OP:
It is always that *sequence* that has some property of randomness.
So it is important that you let the generator alone, seed it only once,
and then let the generator do its magic to come up with the *sequence*
of random numbers. If you reseed the generator all the time, you don't
let the generator build up its randomness.

The thing is: Is 3 random? Is 5 random? Nobody knows. But with some tests
we can decide if the sequence 3 5 1 9 2 (some more numbers) is a pseudo random
sequence or if there is some pattern in it. A good random generator generates
long sequences without a pattern, and that is what we call 'pseudo random numbers'.
So 'randomness' in a computer is some sort of statistical property. And as with
all statistical properties you need a large enough base for testing.
But for this to work you *must not* interfere with the number generating algorithm
as a reseed would do.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
11
by: TreatmentPlant | last post by:
I need to generate a few thousand true random numbers using C++. I have some code now that does alright, but when you plot the results on a graph, you notice patterns, so the numbers are not...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
11
by: Kevin Williamson | last post by:
Hi, I've written a lottery programme in PHP that selects 10 winners a week from approximately 18,000 shares. The problem I have is that of the ten winners one week the same share number was...
2
by: HumanJHawkins | last post by:
I wrote this question a little differently in the MFC group since it is a bit of a different environment than pure C++, but I hope you will forgive the similarities if anyone is reading both...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
16
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.