473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

retrieving random number generator state and other RNG questions

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

Jul 26 '06 #1
5 1948
ilias wrote:
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;
This cast to float is bogus. Try casting to 'double'. On your
Linux, it's possible that 'unsigned long' is larger in size than
'float' (on Windows it's the same).

Generally speaking, you should avoid casts like that (and especially
when posting here). The behaviour of code that uses such casts is
undefined in Standard C++, so you're on your own when you try using
them.
};
void my_srand(unsigned int seed){idum = seed;};

void main()
int 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
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '06 #2
"ilias" writes:
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 ?
That's right. Any generator that doesn't have internal state would be
unable to produce and return any number more than once.

<snip code>

(I don't like global variables unless they are serve a purpose, yours don't,
so why bother fixing something when starting over is probably a better
idea?)

I would find code for a generator I like, I see such code on the net and it
is not hard to find. I would put it in a class that was able to return the
internal state via a member function on demand.


Jul 26 '06 #3
In article <11**********************@b28g2000cwb.googlegroups .com>,
il****@gmail.com says...
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 ?
With the version of rand() in the standard library, yes, that's
correct. I'd consider something along these lines:

#include <iostream>

class PRNG {
mutable unsigned long seed;
static const unsigned long max = 0xffffffff;

unsigned long rand_() const {
seed = (seed * 314159269 + 1) & max;
return seed;
}

public:
PRNG(unsigned long s=0) : seed(s) {}

double rand() const {
return rand_() / static_cast<double>(max);
}

friend operator<<(std::ostream &os, PRNG const &p) {
os << p.seed;
}

friend operator>>(std::istream &is, PRNG &p) {
is >p.seed;
}
};

#ifdef TEST
#include <time.h>

int main() {

PRNG p(time(NULL));

for (int i=0; i<10; i++)
std::cout << p.rand() << std::endl;
return 0;
}

#endif

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 26 '06 #4
Thanks for your suggestions. Just a follow up with the problem.
You were right to bring the casting issue up, although this was not the
problem and I was initially casting to double. Jerry's code is nice but
slower than what I wanted.

So the problem I was having was not in the code itself but on the -O3
compiler attribute. without it (g++ -o foo foo.cpp -lm) it works fine.
Does anyone know what is messed up when optimization is on?

Jul 27 '06 #5
ilias wrote:
>
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 ?
The rand code in Numerical Recipes may or may not be like the code used
to implement rand in your standard library. For checkpoints you really
need the random number generators in TR1, which will also be part of the
next version of the C++ standard. You can find documentation for the
various random number generators in TR1 at
http://www.dinkumware.com/manuals/?m...ge=random.html. For
a more detailed discussion, see my book, "The C++ Standard Library
Extensions", coming soon to a bookstore near you. It's a tutorial and
reference for all of TR1.
Jul 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
8
by: Aaron | last post by:
I need some help writing this function the function returns a list of random non-duplicate integers in a string, 1 parameter indicating the maximum range. string randGen(int maxRange) ...
16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
15
by: felixnielsen | last post by:
This is something i have done before and i know its pretty simple, however i cant remember how it works exactly, and i need it i kinda hurry, so if someone would be so nice to drop a random number...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
2
by: Matthew Wilson | last post by:
The random.jumpahead documentation says this: Changed in version 2.3: Instead of jumping to a specific state, n steps ahead, jumpahead(n) jumps to another state likely to be separated by many...
16
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.