Archived

This topic is now archived and is closed to further replies.

Large Random Numbers

This topic is 5150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All, I''d like to create truly large random numbers with C++. I''m not sure how. Sure, I can always multiply two random numbers. Or alternatively, add two random numbers. But that doesn''t guarantee a relatively correct distribution.. does anyone know how? I saw that there''s a unix C++ command lrand48, but I can''t seem to get it working on MS Visual Studio 6 (guess it''s not there). Any help would be greatly appreciated!!!! Yours sincerely, Udi

Share this post


Link to post
Share on other sites
What''s wrong with multiplication?

Assume that Random returns a value between 0 and 255.

To get a number between 0 and 255: Random

To get a number between 0 and 65535: Random + (Random * 256)

To get a number between 0 and 2^32-1: Random + (Random * 256) + (Random * 256 * 256) + (Random * 256 * 256 * 256)

And so on. Each call to random fills an 8 bit chunk of the final number (or more efficent, do a 32bit chunk at a time). Distribution is just as a good as the original Random function.

Share this post


Link to post
Share on other sites
I''m a vb guy myself, where large data types don''t exactly abound. (So I can''t give any code)

But, from what I''ve seen (what with the meressene twister in particular) is a shifting of bits.

What you might try to do is (if you have some unsigned 64bit int variable, called, for instance "value")
value = rand()<<32 + rand();

That should give you a decent 64 bit value. My only thing would be that, rand is seeded (iirc) by it''s last generated number, so the 2nd half of the value will always be equal to rand seeded by the first half (might not be "random" enough.)

My two cents,
-Michael

Share this post


Link to post
Share on other sites
quote:
Original post by Thr33d
[...]My only thing would be that, rand is seeded (iirc) by it''s last generated number, so the 2nd half of the value will always be equal to rand seeded by the first half (might not be "random" enough.)[...]
Since the meressene twister uses a 640-dword seed (or something like that, its really large), the second number can be MANY different values reguardless of the first number (ie - the entire seed would have to be the same for the second number to come out the same the second time, just the 32bit first number being the same isn''t enough)

Share this post


Link to post
Share on other sites