1

I have generated random numbers in turbo C++ ( DOS) by using random command.
Again, I have generated random number in ubuntu C++ by using srand and rand commands.

Both the random generations are not matching each other ( ie. both generations are different for a particular seed).

Please let me know the reasons behind this problem. Is it possible to get a code behind random command, srand and rand commands?

Parto
  • 15,325
  • 24
  • 86
  • 117
user287594
  • 11
  • 1

1 Answers1

3

Turbo C++ and Gnu G++ (C++) (perhaps LLVM/clang) have different libraries. Their implementations of rand, srand and many (many!) functions will probably be different in HOW they provide the proper result.

Every function in those libraries will produce expected results, open, read, exit and so on. (If they don't, they're not very 'standards conformant').

So rand & srand ARE working properly in each environment... they provide (and seed) random numbers.

HOW they arrive at that result is not defined, there are several methods of creating random numbers (See `Random number generation' for more info on RNG's)

Therefore, even given the same seed value, you cannot expect different RNG libraries to provide identical output. This will be true regardless of which source of libraries you use, Microsoft, Solaris, Linux, *BSD's, OSX and so on.

Only if the exact same method is used to derive the random numbers can you sort-of expect an identical series. Differences in entropy gathering may differ and create perturbations over time. I imagine someone will correct me about the 'sort-of', but as long as you're using the same libaries, you should get the same results from the same seeds. (same same same!)

lornix
  • 2,036