What is Java util. Random. O (n) of next (n)
•
Java
I want to know Java util. Random. Is next (n) linearly proportional to N or a constant? Can someone help me with this or tell me how to determine complexity?
Solution
From document:
According to the documentation, Java util. Random. Next is implemented as follows
synchronized protected int next(int bits) { seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); return (int)(seed >>> (48 - bits)); }
So complexity is O (1)
In marginal note: –
You can use a variety of tools to measure the complexity of micro benchmarks You can find a list of more than here However, if runtime complexity is important to you, you can use fast Mersenne twister (this is an external library to measure runtime complexity, because the Javas random number generator is very fast, but statistically bad)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码