64 bit random number stuff

Started by
4 comments, last by phresnel 13 years, 4 months ago
It seems from extensive googling that Im not the only one who has had a difficult time getting decent 64 bit random number generation...

I managed to throw something together (below) and although it compiles without warnings or errors, the results it produces are not what I expect...

I was wondering - if anyone has 30 seconds free - whether someone would be willing to give me a sanity check, and possibly a clip round the ear if Ive made a daft mistake....

The issue is that it is only returning values in the range of -2147481630 to +2147481630, ie its processing the random numbers as 32 bit instead of 64. Since I have instructed uniform_int to be a 64 bit type, I fail to see why it is not coping with 64 bit. If there was a problem I would have expected the compiler to bark at me....

Any advice (even if its to say: "you're really dumb and this is easy to fix") will be appreciated.



===============================================================


#include <fstream>
#include <iostream>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>
#include <ctime>

using namespace std;

boost::mt19937 gen;




int roll_die() {

boost::uniform_int<int64_t> dist(-9223372036854775807LL, 9223372036854775807LL);
boost::variate_generator<boost::mt19937&, boost::uniform_int<int64_t> > die(gen, dist);
return die();
}

//− 9,223,372,036,854,775,807


int main ()

{

gen.seed(static_cast<unsigned int>(std::time(0)));

int smeg;

long long die;

ofstream datfile("c:\\temp\\stars.dat");

for(smeg=0;smeg<1000000;smeg++)

{

die=roll_die();

std::cout << "hello: " << die << std::endl;

datfile << die << endl;

}

datfile.close();

return 0;

}

============================================================
Advertisement
Quote:int roll_die() {

shouldn't that be
Quote:int64_t roll_die() {

?
Quote:Original post by snoutmate
Quote:int roll_die() {

shouldn't that be
Quote:int64_t roll_die() {

?



Ah-HAH! You spotted the deliberate mistake.

Wow, I cant believe I overlooked that. Any chance an admin can delete this shameful display of blindness please?

Thankyou snoutmate. 1 beer winging its way to you
For anyone who is remotely interested, the code now works and takes just over 19 seconds to produce a file containing 1 million 64 bit ints with uniform distribution. Intel Quad, 8GB Ram, Windows xp 64.
Will test it on one of my linux machines and post the results.

Dont know if thats useful / meaningful to anybody but thought I would share just in case.
If you need to cut down on the time taken then compiling as a 64 bit binary will result in much faster operations on 64 bit integers. The generator used makes a big difference too.

http://beta.boost.org/doc/libs/1_42_0/libs/random/random-performance.html

Mersenne Twister is actually one of the fastest, although the CPU they tested on was a fossil so your machine might produce entirely different results.
Quote:Original post by taz0010
Mersenne Twister is actually one of the fastest, although the CPU they tested on was a fossil so your machine might produce entirely different results.


Somewhere before around 1.37 release or so (don't remember exactly) sequences were different as for some bug somewhere (according to some mail I had with Steven Watanabe), but since that fix, it should generate the same sequence everywhere, and even better, it is conforming to C++0x, which defines for each RNG the result for the 10000th invocation.

This topic is closed to new replies.

Advertisement