Analysis of common methods of generating random numbers in Java

This paper describes the common methods of generating random numbers in Java. Share with you for your reference, as follows:

Antecedents:

Frequently, when testing some functions, you need to create some false data. Every time you go online to check how to generate random numbers. I'll sort out the methods I found this time. I don't have to look everywhere next time.

Several methods of generating random numbers:

1. Use math Random() generates random numbers

Direct paste a few small methods

2. Use Java util. The random class generates random numbers

This paper briefly introduces several methods in the class

method:

Public int nextint(): randomly generate an int type value public int nextint (int n): randomly generate an int type value > = 0 and < n, requiring n > 0 public long nextlong(): randomly generate a long type value public float nextflow(): randomly generate a float type value public double nextdouble() : randomly generate a double type value public double nextgaussian(): randomly generate a double type value conforming to the normal distribution. The value basically swings around 0. Someone has done 2 million cycles and verified that the value is between - 6 ~ 6. Public Boolean nextboolean(): randomly generate a boolean type value

Construction:

public Random() public Random(long seed)

Q: what is seed for?

Seed is the parameter used by random to generate random numbers. The nonparametric constructor uses the current time as the seed every time, which is more random. The constructor with parameters is actually pseudo-random and more predictable.

3. Use system Currenttimemillis() module

For example, if you want to take a random number within 1000, you can use system currentTimeMillis()%1000。

PS: here are several online tools with similar functions for your reference:

Online random number / string generation tool: http://tools.jb51.net/aideddesign/suijishu

Online random character / random password generation tool: http://tools.jb51.net/aideddesign/rnd_password

High strength password generator: http://tools.jb51.net/password/CreateStrongPassword

For more information about Java algorithms, readers who are interested can see the topics on this site: Java data structure and algorithm tutorial, summary of Java character and string operation skills, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

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