Math.random() 用法

发布时间 2023-10-17 14:00:12作者: HHHuskie

Math.random() 可以随机产生一个 [ 0 ,1) (左闭右开)之间的随机数 double类型

int random = (int) (Math.random()*10)     随机产生0-9 之间的数字,包括 0 和 9

Math.random()*(n-m)+m         随机产生 n-m 之间的数字 包括m 不包n

Math.random()*(n+1-m)+m   随机产生 n-m 之间的数字 包括 n 和 m

(int)  Math.random()*(n-m)+m )  强转时,必须加上括号