首页 > 编程语言 > 详细

java的随机数Random

时间:2020-03-04 21:02:43      阅读:74      评论:0      收藏:0      [点我收藏+]

测试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 }

 

java的随机数Random

原文:https://www.cnblogs.com/dabu/p/12412002.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!