java的随机数Random

测试1

 1 package com.lv.study.pm.second;
 2 
 3 public class TestMath {
 4 
 5     public static void main(String[] args) {
 6 
 7         //随机得到一个浮点数Math.random()
 8         System.out.println(Math.random()*100/10);
 9         
10         int it=(int) (Math.random()*1000/10);
11         System.out.println(it);
12         
13         
14     }
15 
16 }

测试2

 1 package com.lv.study.pm.second;
 2 
 3 import java.util.Random;
 4 
 5 public class TestRandom {
 6 
 7     public static void main(String[] args) {
 8 
 9         Random rd=new Random();
10         
11         //随机得到int类型范围的 一个随机数
12         System.out.println(rd.nextInt());
13         
14         //随机得到0-9的数
15         System.out.println(rd.nextInt(10));
16         
17         //随机得到一个布尔值
18         if(rd.nextBoolean()){
19             System.out.println("今天赢钱了 大");
20         }else{
21             System.out.println("今天输了 小");
22         }
23         
24         
25     }
26 
27 }

猜你喜欢

转载自www.cnblogs.com/dabu/p/12412002.html