Random numbers across different programming langua

2019-01-28 17:27发布

问题:

If I use the Random number generator function in different programming languages and I chose the same seed .. Do they have to give me the same random numbers ?

for example I used Java and Perl ... gave different numbers.

回答1:

No, unless the two languages happen to use the same algorithm.



回答2:

Portability across languages is one reason to use your own random number generator. This article gives a very small random number generator whose algorithm has been vetted by experts. You can easily port the generator to any language that supports basic operations on 32-bit unsigned integers. Every implementation should return the same sequence of outputs given the same seeds.



回答3:

Just want to add, there is no such thing as 'random' in computer science.

Random number generation depends on the mathematical function used to generate the number. For example, Java uses 'Linear Congruential generator'.

If two programming languages use exact same function, theoretically they should give same 'random' number.

If you are interested more on how random numbers are generated, There is a good publication, The Art of Computer Programming by Donald Knuth.



标签: random