random number generation

P

Profil1

hi, is this the correct way to generate floating point random numbers >= 0.0
and <= 1.0 that are different each time the program is run?

#include <stdlib.h>
#include <time.h>

srand( (unsigned)time( NULL ) ); //call it only once in a program liftime??
float random_value = rand()/RAND_MAX;
 
V

Victor Bazarov

Profil1 said:
hi, is this the correct way to generate floating point random numbers >= 0.0
and <= 1.0 that are different each time the program is run?

#include <stdlib.h>
#include <time.h>

srand( (unsigned)time( NULL ) ); //call it only once in a program liftime??
float random_value = rand()/RAND_MAX;

I think generation of [pseudo-] random numbers has been covered
extensively in many newsgroups to warrant another discussion on it.
Please use the Web to search the answer to your FAQ.
 
K

Karl Heinz Buchegger

Profil1 said:
hi, is this the correct way to generate floating point random numbers >= 0.0
and <= 1.0 that are different each time the program is run?

#include <stdlib.h>
#include <time.h>

srand( (unsigned)time( NULL ) ); //call it only once in a program liftime??
float random_value = rand()/RAND_MAX;

No

rand() returns an integer
RAND_MAX is an integer

So dividing an integer by another integer gives what?
Knowing that RAND_MAX is the highest value that rand() might
produce, what does this tell us about the outcome of the division.
(eg. if the highest number equals 8, what is the result of
2 / 8
3 / 8
7 / 8
)
 
M

Martin Magnusson

Profil1 said:
hi, is this the correct way to generate floating point random numbers >= 0.0
and <= 1.0 that are different each time the program is run?

#include <stdlib.h>
#include <time.h>

srand( (unsigned)time( NULL ) ); //call it only once in a program liftime??
Yes, it should normally only be necessary to call srand once. However,
the return value of time() will only change once per second, so if you
start two instances of the program within one second, they will have the
same sequence of random numbers.
float random_value = rand()/RAND_MAX;
To be sure that you actually get a float, and not just something rounded
to 0 or 1, you could write

float random_value = static_cast<float>(rand())/RAND_MAX;

instead.

/ martin
 
M

msalters

Martin said:
float random_value = static_cast<float>(rand())/RAND_MAX;

Also consider
const float inv_RAND_MAX = 1.0 / RAND_MAX;
float random_value = rand()*inv_RAND_MAX;

Might be more efficient, multiplication is usually cheaper and
(almost?) never more expensive. I think it's not a legal
optimalization for an IEEE-conforming compiler as the results
might differ a bit, but in this case you don't care about it.

Regards,
Michiel Salters
 
M

Martin Magnusson

Martin said:
Yes, it should normally only be necessary to call srand once. However,
the return value of time() will only change once per second, so if you
start two instances of the program within one second, they will have the
same sequence of random numbers.

If you want to use a higher resolution clock for the random seed, you
can do like this

#include <stdlib.h>
#include <sys/time.h>
#include <iostream>
using namespace std;

int main()
{
struct timeval time;
gettimeofday( &time, 0 );
srand( time.tv_usec );
float random_value = static_cast<float>(rand())/RAND_MAX;
cout << random_value << "\n";
}


/ martin
 
E

E. Mark Ping

To be sure that you actually get a float, and not just something rounded
to 0 or 1, you could write

float random_value = static_cast<float>(rand())/RAND_MAX;

While this is common advice, and often works well in practice, this
can cause serious numerical biases. For instance, if RAND_MAX=65536
and you're selecting from 100,000 elements at random, you'll never
select some of the elements.

boost has done quite a bit to make these issues. See:
<http://www.boost.org/libs/random/>
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top