On math. In Java Random () and Java util. The difference between random()

Today, I suddenly remembered that Java generates random numbers. I tried it on the computer and found a difference. I'll summarize it here;

Call math directly Random () generates a random number between [0,1],

If used

java. util. Random random=new Random(); random. nextInt()

In this way, a long integer random number is generated and is the same as the last one. If it is generated later, it will not be the same, for example:

Is to produce different random long numbers

If you want to use Java util. Random() generates random numbers in a specified range, which requires modular operation and some processing.

You can also use random Nextint (100) generates random numbers within 100. Generally, it is best to use nextint (range). If it is not a large number of operations, you can use math Random, (because he can calculate slowly and round it after floating point)

The above article discusses math. In Java Random () and Java util. The difference of random () is that Xiaobian has shared all the contents with you. I hope it can give you a reference and support more programming tips.

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
分享
二维码
< <上一篇
下一篇>>