1

I want to ask about random number in Java and Matlab.

Math.random in Java and rand in Matlab has a same meaning or both is different? If different meaning, what the difference ?

Patrick
  • 91
  • 1
  • 1
  • 12

3 Answers3

6

For Matlab, refer to http://www.mathworks.com/company/newsletters/news_notes/pdf/Cleve.pdf which explains how the multiplicative congruential generator works in Matlab.

For Java, refer to http://www.javamex.com/tutorials/random_numbers/java_util_random_algorithm.shtml#.VsMAw3WLSkA which explain how the linear congruential generator works in the Java Utils class for random number generation.

Both are essentially the same algorithm where Matlab's MCG is a special case of LCG, see here: https://en.wikipedia.org/wiki/Linear_congruential_generator

And yes, C++ (Borland), Java.Utils, Matlab language uses essentially the same algorithm, because it is efficient - it is extremely memory efficient, it has a flat linear distribution (i.e. pseudo-random) -> but it is a poor quality pseudo-random, because of the serial correlation.

BUT there are better algorithms out there, and different ones, take Python for example, uses Mersenne Twister algorithm for its PRNG, but the perceived result is much less random, take a read at this: Random is barely random at all?

Community
  • 1
  • 1
GameOfThrows
  • 4,510
  • 2
  • 27
  • 44
  • 1
    you are welcome, I believe the current known best in the world PRNG algorithm is the WELL (http://www.iro.umontreal.ca/~panneton/WELLRNG.html). Depending on what you are using random for, often times Matlab/Java.Utils should suffice, but if you are doing something like Monte-Carlo simulations, I think you might want to load up a library that uses the Mersenne Twister of XOR shift algorithm for more accurate interpretations. – GameOfThrows Feb 16 '16 at 11:28
  • Great answer. Thanks a bunch. – rayryeng Feb 16 '16 at 16:11
  • In your last paragraph, the problem is with people's misconceptions about randomness rather than the quality of Mersenne Twister. MT's output behaves closer to the way real randomness ought to rather than how people mistakenly think it ought to. A paper back in the 80's showed that for an integer PRNG, *not* observing *any* duplicates by the time you hit `~3 * sqrt(MAX_INT)` constituted a strong (p > .99) statistical test of non-randomness. Given 32 bit integers, that meant a statistician could catch you after ~200k values, when most people assumed the full cycle of ~2B was usable. – pjs Feb 16 '16 at 18:13
  • @pjs exactly, completely agreed. – GameOfThrows Feb 17 '16 at 09:04
2

Both return uniformly distributed pseudorandom numbers between 0 and 1. The algorithm for generating the numbers and their quality is probably different but the interface is the same.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
2

There are many different random number generators in MATLAB. For full details, see the reference page for rng. The default in serial MATLAB is Mersenne Twister (Twister is not a suitable choice for parallel random number generation, so for Parallel Computing Toolbox workers, and on the GPU, the default is Combined Multiple Recursive). On the GPU, additional random number generators are available that are of high quality and highly suited to GPU execution.

One thing to note: MATLAB very carefully sets things up so that when you start a fresh MATLAB process, the very first invocation of rand will always give you the same result (for the same release of MATLAB). This is in contrast to Java, which shuffles the seed if you use Math.random().

MATLAB gives you multiple powerful ways to control the random number streams in use via the rng function, and the methods of the RandStream objects.

Edric
  • 23,676
  • 2
  • 38
  • 40