I am trying to implement the random number generator defined in this answer. There is some ambiguity, at least from my knowledge, as to how the first line, static unsigned long x=123456789, y=362436069, z=521288629;
should be implemented, since it is shown outside the function. I assumed that it was intended as a class member and implemented it thus:
class rng2{
public:
unsigned long x, y, z;
rng2() : x(123456789), y(362436069), z(521288629) {}
unsigned long xorshf96(void) { //period 2^96-1
//static unsigned long x=123456789, y=362436069, z=521288629;
unsigned long t;
x ^= x << 16; //BUS ERROR, debug mode
x ^= x >> 5;
x ^= x << 1;
t = x;
x = y; //SEG FAULT, release mode
y = z;
z = t ^ x ^ y;
return z;
}
};
int main ()
{
rng2 rand2;
rng2 * prand;
for(long d =0; d < 10000; d++)
cout << "\n" << (*prand).xorshf96();
}
For some reason, this gives me errors at the noted locations, depending on which mode I compile with. However, if I comment out the member variable and the constructor and use the static variable instead, everything works. If this is the correct code, I don't see why it was shown differently at the link, and either way, I don't know why the error is happening.
You're using *prand, but not initializing prand.