How to compare "random" generators in different languages ​​(for example, Java and C ++)?

Despite the strange title, I want to ask a legitimate question: which method generated the numbers more random: Java Random() class or Math.random() , or C ++ rand() ?

I heard that PHP rand() pretty bad, i.e. if you compare its results, you can clearly see the pattern; Unfortunately, I do not know how to draw a map in C ++ or Java.

Also, just out of interest, what about C #?

+8
java c ++ random
source share
5 answers

Both Java and C ++ generate pseudo-random numbers that either:

  • adequate to the task for anyone who is not a statistician or a cryptographer (a) ; or
  • extremely inadequate to those two classes of people.

Everyone is honest, if you are not in one of these classes, pseudo-random number generators are fine.

Java also has SecureRandom , which tries to provide cryptoclassical non-determinism (I can not comment on the veracity of this argument), and C ++ now has a much wider range of random number generation capabilities than just rand() For more details see <random> .

Specific operating systems may provide entropy sources for random number generators, such as CryptGenRandom for Windows or reading /dev/random for Linux. Alternatively, you can add entropy using random events such as user input time.


(a) May actually contain traces of other types of tasks that are not statisticians or a cryptographer :-)

+9
source share

java.util.Random (which is used inside Math.random() ) uses a Linear congruent generator , which is a pretty weak RNG, but enough for simple things. For important applications, use java.security.SecureRandom .

I do not think that the specifications of the C or C ++ language prohibit the use of the algorithm for rand() , but in most implementations, LCG is used. C ++ 11 has added new APIs that provide better randomness.

+5
source share

There is a very good document that can be found on the Internet, made by one of the world's experts in random number generators.

Here is a document

The first part of the document is a description of the tests you can skip if you are really not interested. Page 27 shows the results of various tests for many generators, including Java, C ++, Matlab, Mathematica, Excel, Boost, ... (They are described in the text).

The Java generator seems to be a little better, but both are not some of the best in the world. MT19937 from C ++ 11 is already much better.

+3
source share

PHP uses seed. If the seed is the same two different times, the rand () function will ALWAYS output the same thing. (For example, this can be very bad for tokens). I do not know for C ++ and Java, but there is no true coincidence, which makes quality assessment difficult. Security does not rely on such features.

0
source share

I don’t know a single language where random numbers are really random - I’m sure that such a thing exists, but, as a rule, it β€œyou insert the seed, and you get the sequence that the seed gives”. This is normal if you want to create a simple "shoot" game, a basic poker game, a roulette simulator for home use, etc. But if you have money relying on a game that is really random (for example, you give money based on the results of certain sequences), or your secret files rely on your random numbers, then you definitely need some other mechanism for finding random numbers .

And there are some "true" random number generators. They do not produce seed, so predictability based on the number (number) you received last time is low. I am not saying that it is zero, because I am not sure that you can get it even because of the sampling of radio waves at an unused radio frequency, radioactive decay or any last method of generating true random numbers.

0
source share

All Articles