JAVA学习笔记——Random类

xiaoxiao2021-02-28  135

1、指定范围( start - end )内的随机数     int num = (int) (Math.random() * (end - start + 1)) + start ;

2、Random类:产生随机数的类 (1) 构造方法     public Random() : 没有给种子,使用的是默认种子,即电脑此时时间的毫秒值     public Random(long seed) :给出指定种子     注意:指定种子的作用是每次得到的随机数是相同的,只要种子不变,每次的随机数就不变。

(2) 成员方法     public int nextInt() : 返回的是int范围内的随机数     public int nextInt(int n) : 返回的是[0 , n)范围内的随机数(包左不包右)     例如: int n = (new Random()).nextInt(100) ; //生成的是0-99的数字,不包括100

通常,我们都用Math.random()来实现随机数。

转载请注明原文地址: https://www.6miu.com/read-19231.html

最新回复(0)