Velocity Reviews > C++ > random number generation

# random number generation

Profil1
Guest
Posts: n/a

 03-30-2005
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;

Victor Bazarov
Guest
Posts: n/a

 03-30-2005
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.

Karl Heinz Buchegger
Guest
Posts: n/a

 03-30-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

Martin Magnusson
Guest
Posts: n/a

 04-04-2005
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;

/ martin

msalters
Guest
Posts: n/a

 04-04-2005

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

Martin Magnusson
Guest
Posts: n/a

 04-04-2005
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

E. Mark Ping
Guest
Posts: n/a

 04-04-2005
In article <1112600410.38806c8dd45da8a3a4a37b6ab0a2ed61@teran ews>,
Martin Magnusson <(E-Mail Removed)> 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
(E-Mail Removed)

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post VK Javascript 15 05-02-2010 03:43 PM dpi VHDL 4 03-26-2010 10:31 AM globalrev Python 4 04-20-2008 08:12 AM ANM Java 2 03-07-2004 07:18 AM mescaline C++ 4 09-10-2003 09:01 PM