随机数Random的两种写法

Math.random:   这是一个默认方法,不接受任何参数

随机范围: 0<Math.random ( ) < 1.0

double r = Math.random ( ) ;        //默认是double类型,转别的类型需要强转


int a = (int) (Math.random( )*100);    //从0到100之间 不包含100
//Math.random后面的括号里面不能写东西


double x = Math.random ( )+15;    // [ 15,15+1)

double y = Math.random ( )*15;    //  [ 0,15)

double o = Math.random ( ) *15 +10; 

double p = (Math.random ( )*15) +10;

o和p的写法虽不同 但是 范围是一样的 都是 [ 10,25) 

 左包含 右不包含



Random方法
使用这个方法需要先new 一个Random类型的对象

Random r = new Random();        //用Random方法需要新建一个对象

int a = r.nextInt ( 100 ) ;         //  [ 0,100 ) ; 

int b = r.nextInt(180)+121;    // (0到180)+121  范围是121到301  不包含301的整数

double c = (r.nextDouble ( )*121)+15;    //15到135的小数


double d = (r.nextDouble ( )+121)+15;    //136到136+1的小数





猜你喜欢

转载自blog.csdn.net/y2418230874/article/details/80027896