Math.random() 随机数

来源:互联网 发布:物联网域名交易平台 编辑:程序博客网 时间:2024/06/06 01:57
 /*
 * 需求1:产生10个   [0,1) 之间的随机数 *   2:   产生10个[0,10)之间的随机整数   *   3.产生10个    [10,20)之间的随机整数 *   4.产生 10个 [10,20] 之间的随机整数  等价于  [10,21) * */public class TestMath {public static void main(String[] args) {//1:产生10个   [0,1) 之间的随机数for(int i=0;i<10;i++){//System.out.println(Math.random()+"\t");}//2:产生10个 [0,10)之间的随机整数 for(int i=0;i<10;i++){int a=(int) (Math.random()*10);//System.out.print(a+"\t");}//3.产生10个    [10,20)之间的随机整数for(int i=0;i<10;i++){int a=(int) (Math.random()*11+9);//System.out.print(a+"\t");}//4.产生 10个 [10,20] 之间的随机整数  等价于  [10,21)for(int i=0;i<10;i++){int a=(int) (Math.random()*11+10);System.out.print(a+"\t");}}}