Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ rand and srand gets different output on different machines

I wanted to generate a random integer, so I used C++ rand(void) and srand(int) functions:

int main(){
     srand(1);
     cout << rand() << endl;
     return 0;
}

OK, it suits my needs. Each time I execute it I get same result, which I like it!
But there is a problem. When I executed it on my computer I got 16807 as output. But when I executed it on another machine, I got 1804289383.

I know that rand() and srand(int) have a simple implementation similar to this:

static unsigned long int next = 1;

int rand(void) // RAND_MAX assumed to be 32767
{
    next = next * 1103515245 + 12345;
    return (unsigned int)(next/65536) % 32768;
}

void srand(unsigned int seed)
{
    next = seed;
}

So why? Is it possible that rand() has different implementations on multiple machines? What should I do?

I want to modify the other machine in such a way that I get 16807 from that machine too.
Please note that I love the rand implementation on my computer. Please show me a way that other machine gets same result with mine.

Thanks in advance.

like image 457
Pro.Hessam Avatar asked Dec 22 '22 09:12

Pro.Hessam


2 Answers

Yes, rand() has different implementations; there's no requirement for them to be identical.

If you want consistent sequences across implementations and platforms, you can copy the sample implementation from the C standard section 7.20.2. Be sure to rename both rand and srand so they don't collide with the standard library's versions. You might need to adjust the code so the types have the same size and range across the implementations (e.g., use uint32_t from <stdint.h> rather than unsigned int).

EDIT: Given the new information from the comments, it looks like the requirements are different from what we thought (and I'm still not 100% clear on what they are).

You wants to generate random numbers on two systems consistent with a stored file that you've generated on one system, but you're unable to transfer it to the other due to network issues (the file is about a gigabyte). (Burning it to a DVD, or splitting it and burning it to 2 CDs, isn't an option?)

Suggested solution:

Write a custom generator that generates consistent results on both systems (even if they're not the same results you got before). Once you've done that, use it to re-generate a new 1-gigabyte data file on both systems. The existing file becomes unnecessary, and you don't need to transfer huge amounts of data.

like image 94
Keith Thompson Avatar answered Dec 24 '22 00:12

Keith Thompson


I think it's because int/unsigned int on your two platforms is a different size. Are ints/unsigned ints the same number of bytes on both machines/OSes you're compiling on? What platforms/compilers are you using?

Assuming the same rand/srand implementation, you need to use datatypes of the same precision (or appropriate casting) to get the same result. If you have stdint.h on your platform, try and use that (so you can define explicit sizes, e.g. uint32_t).

like image 28
Joe Avatar answered Dec 24 '22 00:12

Joe