By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

random number generation

P: n/a
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;
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Profil1 wrote:
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.
Jul 23 '05 #2

P: n/a
Profil1 wrote:

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
)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3

P: n/a
Profil1 wrote:
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
Jul 23 '05 #4

P: n/a

Martin Magnusson wrote:
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

Jul 23 '05 #5

P: n/a
Martin Magnusson wrote:
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
Jul 23 '05 #6

P: n/a
In article <1112600410.38806c8dd45da8a3a4a37b6ab0a2ed61@teran ews>,
Martin Magnusson <martin@-xx-blecket-xx-.org> wrote:
Profil1 wrote:
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;


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/>
--
Mark Ping
em****@soda.CSUA.Berkeley.EDU
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.