I
ilias
Hi,
I am running a C++ code in multiple environments/clusters. Until now I
was using the standart rand() function as RNG. The problem is that I
want to be able to stop my code and start it again every now and then
(i.e. to have checkpoints), and to do that I need the state of the RNG
to reseed it. Reading the rand() code (in "Numerical Recipes in C") I
understand this is not possible. Am I right ?
So I am trying to make a RNG that is fast, compatible in windows, linux
and darwin, and at least of equal statistical value as rand() - I know,
hard to beat -. The following code works in windows, but not quite
in linux. More specifically, (a)it produces a -1 as the first random
number, then the same 9 numbers as in windows and (b) any attempt to
reseed it fails.
Any help would be greatly appreciated. Thanks!
ilias
CODE:
************************************************
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace::std;
static unsigned long jflone = 0x3f800000;
static unsigned long jflmsk = 0x007fffff;
unsigned long idum,itemp;
double my_rand(){
idum = 1664525L*idum + 1013904223L;
itemp = (jflone | (jflmsk & idum)); //(*)
return( *(float *)&itemp)-1.0;
};
void my_srand(unsigned int seed){idum = seed;};
void main()
{
int i;
my_srand(0);
for (i=0;i<10;i++)
{
my_rand();
cout << my_rand() <<"\n";
}
unsigned int temp;
temp =idum;
cout << "\nidum is " << idum << " and temp is " << temp ;
cout << "\nnext number would be " <<my_rand() <<" and next idum
" << idum;
my_srand(temp);
cout << "\nafter init.srand idum is " << idum << " random
number is " << my_rand() << " and new idum " << idum;
*************************************************************************
LINUX OUTPUT:
-1
0.626257
0.947852
0.365433
0.698232
0.880344
0.633586
0.179411
0.552378
0.577698
idum is 2768872580 and temp is 2768872580
next number would be 0.0753331 and next idum 2254235155
after init.srand idum is 2254235155 random number is 0.725771 and new
idum 2254235155846930886
I am running a C++ code in multiple environments/clusters. Until now I
was using the standart rand() function as RNG. The problem is that I
want to be able to stop my code and start it again every now and then
(i.e. to have checkpoints), and to do that I need the state of the RNG
to reseed it. Reading the rand() code (in "Numerical Recipes in C") I
understand this is not possible. Am I right ?
So I am trying to make a RNG that is fast, compatible in windows, linux
and darwin, and at least of equal statistical value as rand() - I know,
hard to beat -. The following code works in windows, but not quite
in linux. More specifically, (a)it produces a -1 as the first random
number, then the same 9 numbers as in windows and (b) any attempt to
reseed it fails.
Any help would be greatly appreciated. Thanks!
ilias
CODE:
************************************************
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace::std;
static unsigned long jflone = 0x3f800000;
static unsigned long jflmsk = 0x007fffff;
unsigned long idum,itemp;
double my_rand(){
idum = 1664525L*idum + 1013904223L;
itemp = (jflone | (jflmsk & idum)); //(*)
return( *(float *)&itemp)-1.0;
};
void my_srand(unsigned int seed){idum = seed;};
void main()
{
int i;
my_srand(0);
for (i=0;i<10;i++)
{
my_rand();
cout << my_rand() <<"\n";
}
unsigned int temp;
temp =idum;
cout << "\nidum is " << idum << " and temp is " << temp ;
cout << "\nnext number would be " <<my_rand() <<" and next idum
" << idum;
my_srand(temp);
cout << "\nafter init.srand idum is " << idum << " random
number is " << my_rand() << " and new idum " << idum;
*************************************************************************
LINUX OUTPUT:
-1
0.626257
0.947852
0.365433
0.698232
0.880344
0.633586
0.179411
0.552378
0.577698
idum is 2768872580 and temp is 2768872580
next number would be 0.0753331 and next idum 2254235155
after init.srand idum is 2254235155 random number is 0.725771 and new
idum 2254235155846930886