seeding function

A

AngleWyrm

Ok, I know that srand( time(NULL) ) isn't such a good idea, but I do it
anyway. Why? The clock is an easy source of randomness. And I've got this
sneaky suspicion that I'm not the first guy to commit this foul atrocity
against the finer design principles of RNGs in general...

So here's me thought: Design a seeding function that has only one purpose --
and that purpose is to use time(NULL) as it's input, and generate a
reasonably random output for seeding a random number generator.

As of right now, the value of time(NULL) on my machine is: 1095397643 (1.095
billion), and increments once per second. the return value is of time_t,
which according to www.cplusplus.com is generally defined as a long. It's
four bytes on my machine.

USAGE...here's where things differ markedly from "normal" RNGs:

It occurs to me that this function doesn't really have to return a different
series ten thousand years from now. Most people are going to use it in rapid
succession several times over the course of an hour or three -- usually the
same hour or three in a day. Even more to the point, RAND_MAX is usually
defined as 32768, or 2^15.

Thus what we have here is a function that maps a spread of numbers from one
domain to another, and we are reasonably sure the domain to map to is the
2^15 random numbers in the range [0..32768).

Now another thing to notice: We don't need thirty two thousand random seeds.
Most of the time, only the first number is used, and far less often the
first dozen or hundred.

How then can I approach the problem of selecting just one random number
given a sequential seed? It occurs to me to perhaps use some standard number
sequence, maybe offset or modified?
 
S

Stijn Oude Brunink

I'm not sure wether you want to build a random number generator or you just
want to use one

If the last is the case then I would use a standard random number generator
package. Such issues about seeds have been dealt with and you do not want to
reinvent it.

you can find a good package on www.netlib.org (ranlib) A newer generator is
mersene twister, look for it on the web

I would be carefull with just simply using rand()

stijn
 
A

AngleWyrm

Stijn Oude Brunink said:
I'm not sure wether you want to build a random number generator or you just
want to use one

Specifically, I am interested in developing a seeding function for
std::rand(), which would serve as a replacement for srand().
If the last is the case then I would use a standard random number generator
package. Such issues about seeds have been dealt with and you do not want to
reinvent it.

Actually, I do want to reinvent it -- the issues I'm interested in don't
appear to have been dealt with. The mersenne twister uses a 32-bit seed, at
the very least. Seeding the mersenne twister with time(NULL) (~1.09 billion)
every time isn't going to be much of an improvement.

State information for std::rand() can be stored, so that multiple instances
may have different sequences, and the user doesn't use up dimensionality on
several purposes. Here is an improvement on std::rand():

////////////////////////////////////////////////////////////////////////////
////
// marginal improvement on std::rand()
// This functor stores state information,
// so that multiple instances can have different seeds.
class rng_class {
public:
void seed( unsigned int s ){ current = s; };
unsigned int operator()(unsigned int range)
{
if ( range == 0 ) return 0; // only one possibility.

// save global rand state, and restore this instance's state
unsigned int global_state = rand();
srand( current );

// handle cases where range doesn't evenly divide RAND_MAX,
unsigned int bucket_size = RAND_MAX / range;
unsigned int result;
do {
current = std::rand();
result = current / bucket_size;
} while ( result >= range );

srand( global_state ); // restore global state

return result;
}
protected:
unsigned int current;
};


The problem isn't the generators, it's the use of time(NULL) for seeding.
This is the reason for this thread: The generators are ok.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Repetitive Function 3
Seeding random number generator 3
Function for primes 0
seeding random numbers 18
Keypress function 0
How do i Do this function(dealing with arrays) 1
Help with a function 8
Function Help 1

Members online

Forum statistics

Threads
474,266
Messages
2,571,087
Members
48,773
Latest member
Kaybee

Latest Threads

Top