0

Any idea how to create the same random numbers in C/C++ and Java? With seed, we can create random number in C++ and Java. But the created number looks different value.

<in C++>
srand(32323);
printf("rand : %d\n", rand());


<in Java>
Random rnd = new Random();
rnd.setSeed(32323);
nextInt() 

or 

Random rnd = new Random(32323);
byte []buf = new byte[12];
rnd.nextBytes(buf);

I want to get same random number against two languages when the same seed is used.

  • 2
    In general, the best way to "sync" PRNGs between two programs in different languages is to implement the same PRNG in both languages. In any case, the `rand` function should not be used in C++, notably because `rand`'s algorithm is unspecified in both C and C++. See also: https://stackoverflow.com/questions/52869166 ; https://stackoverflow.com/questions/59829276 – Peter O. Mar 08 '22 at 07:59

0 Answers0