# Problem building 64bit rand

• 05-31-2011
Problem building 64bit rand
THIRD EDIT: Feel free to delete this problem. Turns out the object hadn't been allocated... so when I'd access it's vars they weren't allocated either. Arg.

I've been building a transposition table in C++ so it came up that I needed 64 bit random numbers (don't ask..) so I built this function:

Code:

```long long int Ttable::randomLong() {   int firstHalf, secondHalf;   long long int newRand;   firstHalf = rand();   secondHalf = rand();   //copy the first value into the top 32 bits   newRand = firstHalf;   newRand = newRand << 32;   //XOR in the second half covering up all the new zero bits   newRand = newRand ^ secondHalf;   return newRand; }```
And all of the variables I assign to it such as:

Code:

`white = randomLong();`
or for testing
Code:

`cerr << randomLong();`
give me segfaults when I go to use em. I know I'm probably missing something simple. If you can't find anything and know of a good *default* 64 bit random number generator that'd also.. probably be fine.

While I wait I'll go recheck the code with a standard random function... but that should work fine.

I haven't gotten any useful info from GDB or valgrind other than helping me find that these vars are the ones segfaulting.

Thanks
-E

EDIT:
I changed to rand() and it still hasn't worked. I must be doing something incorrect at declaration?
Code:

`long long int white;`
I'm going to back up the files and try out the "standard" 64 bit int, should be int64_t. I hadn't thought that that could be a problem :S. Will report back if change is successful.

SECOND EDIT:
Didn't change anything. I still don't see what it's doing that would cause an allocation problem. These aren't pointers :S.
• 05-31-2011
phantomotap
O_o

That code can't cause a segfault.

I don't know where it is because my "Foo" has malfunctioned, but somewhere you have a problem. Be happy you have new knowledge of it.

Soma
• 06-01-2011
Elysia
This speaks volumes for using an approach that makes sure that an object is fully constructed precisely after the constructor is finished. Otherwise you are going to have to add extra checks to make sure the object isn't in a zombie state in all functions or risk doing what you just did.
• 06-01-2011
Inanna
This is a little off topic, but if you have a compiler with C++0x or C++03 TR1, the mt19937_64 class will give you 64 bit random numbers with the mersenne twister algorithm. It is a much better way than hacking 64 bits together with rand().
Code:

```#include <iostream> #include <random> using namespace std; int main() {     mt19937_64 gen;     for (int x = 0; x < 10; ++x)     {         cout << gen() << endl;     } }```