简体   繁体   中英

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.

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 (eg, 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.

The C and C++ specifications do not define a particular implementation for rand or srand . They could be anything, as long as it is somewhat random. You cannot expect consistent output from different standard libraries.

The rand implementations can be different. If you need identical behavior on different machines, you need a random number generator that provides that. You can roll your own or use someone else's.

I am not sure if the random generators in the C++0x library suffices. I think not. But reading the standardeese there makes my head spin.

Similarly, I'm not sure whether the Boost Random library suffices. But I think it's worth checking out. And there you have the source code, so at worst it can serve as basis for rolling your own.

Cheers & hth.,

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, eg uint32_t).

Also, there are different Pseudo-RNG algorithms (eg LCG vs Mersenne Twister)

http://en.wikipedia.org/wiki/Random_number_generation

C compiler on your first machine may use one, and the second machine may use another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM