C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

retrieving random number generator state and other RNG quest

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
ilias
Guest





PostPosted: Wed Jul 26, 2006 9:10 am    Post subject: retrieving random number generator state and other RNG quest Reply with quote



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 Smile -. 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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.