   How to generate a random 64 bit integer
  Hi,I´m trying to create a random 64 bit integer. The function rand() only provides 15 bits on most platforms. So I came up with this:inline unsigned __int64 TGRand64(){   unsigned __int64 Output = (rand()&0x7FFF);   Output |= ((rand()&0x7FFF)<<15);   Output |= ((rand()&0x7FFF)<<30);   Output |= ((rand()&0x7FFF)<<45);   Output |= ((rand()&0x000F)<<60);   return Output;}But I don´t think the bitshifts will work. I can remember I tried this years ago and the bitshift operation cannot be larger than 31 if I´m correct. Also, my compiler is giving me warnings.Only thing I can think of is using multiplication instead of bitshifts. But isn´t there a cleaner, better way of doing this? I feel like I´m overcomplicating things.Peter   
  Did you try casting the results of the rand() calls to __int64 before doing the shifts?   ;  Yeah, be careful with that and make sure to cast something in the calculation to 64-bit so you're assured it's 64-bit - I remember GCC once throwing a warning at me for not doing that. This situation seems to be true with all integers operations though, so switching to multiplications wouldn't change it anyways.And before somebody tries to correct me: this is in the case that long is 32-bit =P If long is 64-bit, then operations will expand to 64-bit, because it can be casted up to a long. It's to a long long where it won't be casted automatically. Explicit casting will never hurt in either case anyways.   ;  Automatic integer promotions happen up to int, not long.   ;  Since you already use several |= operators and shifts, you could as well drop rand() alltogether, replace |= by ^|, forget about the last line, and use well-known shift constants for a higher speed, higher quality random number generator.For example, <<16 >>5 <<17 or <<13 <<13 >>19 are "well known numbers" that will work for 64 bit random numbers.There are good and bad values for the shifts, and they depend on some exalted math theory that I'm unable to understand personally. However, that doesn't matter much if you only want to implement it, since there are a couple of papers around that give you dozens of constants that work for either 32 or 64 bit generators.Google for "Marsaglia xor shift generator" to find both the papers and implementations.   ;  Thanks a lot for the help guys, I casted before shifting and it works now.After posting the first message I tried multiplication too, and that indeed did not work either.Best wishes,Peter   ;  You could have used unions or even pointers to do that. Example using unions:union _64BitsValStruct {  UINT64 Output64;  WORD   RandVal[4]; };64BitsValStruct Output;Output.RandVal[0] = (rand()&0x7FFF)Output.RandVal[1] = (rand()&0x7FFF)Output.RandVal[2] = (rand()&0x7FFF)Output.RandVal[3] = (rand()&0x7FFF)// Output.Output64 now contain the random data...Also, why are u shifting by increment of 15 instead of 16???   ;  Because rand() function returns only 15 bits (values from 0 to 32767), not 16 bits.   ;  I would do:unsigned __int64 Output = rand();Output <<= 15;Output ^= rand();Output <<= 15;Output ^= rand();Output <<= 15;Output ^= rand();Output <<= 15;Output ^= rand();;  Use robust and generic random generators, such as Boost.Random.   