一、Java随机数的产生方式
在Java中,随机数的概念从广义上将,有三种。
1、通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字。
2、通过Math.random()返回一个0到1之间的double值。
3、通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大。
二、java.util.Random类有两种方式构建方式:带种子和不带种子
1、不带种子:此种方式将会返回随机的数字,每次运行结果不一样。
2、带种子:无论程序运行多少次,返回结果都是一样的。
三、 Random description
public class Random
- extends Object
- implements Serializable
Random()
Creates a new random number generator.Random(long seed)
Creates a new random number generator using a singlelong
seed:- extends Object
An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.)
If two instances of Random
are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers.
public int nextInt(int n)
- Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generators sequence.
源码下载:Java生成指定长度的随机字符串源代码
参考:http://www.huomo.cn/developer/article-197ce.html