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 ?
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 ?
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?
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.
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 useMath.random()
.MATLAB gives you multiple powerful ways to control the random number streams in use via the
rng
function, and the methods of theRandStream
objects.