Seed grain consistency between physical machines

I am not quite sure how to formulate this question, but I could not find other similar ones.

Let's say I have this code:

srand(1);
srand(SOME_DEFINED_CONST_INT);

If I run this executable on several different physical machines, is the sequence consistent rand()between them? those. if I get 1, 4, 6, 3, 4on one machine, do I always get the same sequence on others?

If so , how can this be proved? Is it part of the standard?

If not , can I do something to do this?

+3
source share
4 answers

, ​​. C. , , . - , .

+2

, . , srand() Linux, - :

POSIX 1003.1-2003 rand() srand(), , , .

       static unsigned long next = 1;

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

       void mysrand(unsigned seed) {
           next = seed;
       }
+3

I will add that if you work under Windows, if you take exe and move between machines, srand WILL generates the same numbers, because the srand implementation is specific for developers, but you will always use the same developer’s runtime (therefore, if you using Microsoft C ++, you will use Microsoft srand, and MS will probably not change its implementation of srand today or tomorrow). The same goes for Linux. Your srand will always be one of glibc. If they do not change it in glibc, the numbers will be the same.

+1
source

All Articles