Java初学习-随机数的产生

来源:互联网 发布:编程教育进入中小学 编辑:程序博客网 时间:2024/04/29 02:38
public static double random()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

Math.random()是令系统随机选取大于等于 0.0 且小于 1.0 的伪随机 double 值

返回指定范围的随机数(m-n之间)的公式:  Math.random()*(n-m)+m

例如:

class IsUpper{    public static void main(String[] args){        char ch;        ch=(char)(Math.random()*128);        if(ch >= 'A' && ch <= 'Z')            System.out.println("Yes!");        else            System.out.println("No!");    }}


0 0
原创粉丝点击