Random

  • Random类的实例用于生成随机数,java.util.Random;
//导包import java.util.Random
//用法Random str = new Randomint number = str.nextInt();
  • 下面依次给出Random的具体方法:
nextBoolean();//返回下一个伪随机数,从这个随机数发生器的序列中均匀分布boolean值//返回true/false,跟抛硬币一样正反面两种可能
nextInt();//返回下一个伪随机数,从这个随机数发生器的序列中均匀分布int值//根据int数据类型的范围生成随机数
nextInt(int bound);//返回随机的,均匀分布在所接受int值和零之间,可以用户自定义[0,n]//包含0,不包含int值
nextDouble();//返回随机浮点数,随机数范围介于[0.0,1.0]之间
nextFloat();//返回随机浮点数,与nextDouble();效果相同,但是精度没有double类型高
nextLong();//返回在long类型数值范围内的随机数
  • 展示效果:
public static void main(String[]args){Random str = new Random();System.out.println("==============nextInt()=============");for(int i=0;i<5;i++){int num = str.nextInt();System.out.print(num);System.out.print("  ");}System.out.println();System.out.println("==============nextInt(int bound)=============");for(int i=0;i<5;i++){int num = str.nextInt(30);//返回0~30之间的随机数,调用nextInt(int bound)方法System.out.print(num);System.out.print("  ");}System.out.println();System.out.println("==============nextFloat()=============");for(int i=0;i<5;i++){float num = str.nextFloat();System.out.print(num);System.out.print("  ");}System.out.println();System.out.println("==============nextDouble()=============");for(int i=0;i<5;i++){double num = str.nextDouble();System.out.print(num);System.out.print("  ");}System.out.println();System.out.println("==============nextLong()=============");for(int i=0;i<5;i++){long num = str.nextLong();System.out.print(num);System.out.print("  ");}System.out.println();System.out.println("==============nextBoolean()=============");for(int i=0;i<5;i++){boolean num = str.nextBoolean();System.out.print(num);System.out.print("  ");}}
输出效果展示:==============nextInt()=============
-1562032916  -136884251  1558462491  -754120168  187213465
==============nextInt(int bound)=============
8  25  25  27  28
==============nextFloat()=============
0.018086076  0.8617662  0.81750375  0.33476055  0.53433836
==============nextDouble()=============
0.8285739850681495  0.4601760428757987  0.20901287422161663  0.8094986555712121  0.2043064974815323
==============nextLong()=============
2876650613484644222  7747307292537564799  -8489012853938487075  -7621584888494178006  7536392212767868692
==============nextBoolean()=============
true  false  true  false  true

Random自定义生成随机数区间

//产生[min,max]之间的随机数--->str.nextInt(max-min+1)+min
public static void main(String[] args){Random str = new Random();for(int i=0;i<5;i++){int num = str.nextInt(7)+1;//生成[1,7]之间随机数    System.out.print(num);System.out.print("  ");}}

Math.random()

  • 用法:

int  num = (int)(Math.random()*2+1)
//以上代码即设置一个随机1到3(取不到3)的变量num。
  • 公式:
  • Math.random()*(max-min)+min
  • 生成大于等于min小于max的double型随机数;
     for(int i=0;i<5;i++){int num = (int)(Math.random()*(4)+1);//生成[1,5)之间随机数    System.out.print(num);System.out.print("  ");}

Random和Math.random()相关推荐

  1. Java 基础之 Random类和Math.random()方法

    Random类 import java.util.Random;public class randomTest {public static void main(String args[]){Rand ...

  2. JavaScript Math.random()方法介绍

    随机法 (Random Method) The JavaScript Math.random() method is an excellent built-in method for producin ...

  3. 外文翻译 | 你以为你会用Math.random() ? 不,你不会……

    文章目录 制造动画效果 HTML CSS JavaScript 电子音乐 HTML SCSS CoffeeScript 随机显示图片 HTML CSS JavaScript 随机背景颜色 Pug SC ...

  4. 控制台输入与Math Random的基本使用

    控制台输入 import java.util.scanner; //引入scanner public class Test {public static void main(String[] args ...

  5. js笔记之Math random()、ceil()、floor()、round()

    本文转自:http://blog.sina.com.cn/s/blog_7e4815650100wrfz.html JavaScript: The Definitive Guide, 4thEditi ...

  6. javascript中Math.random()产生随机数总结

    用JS的随机数生成函数random()配合其他数学函数可以限制随机数的取值. JS的随机数函数及相关函数: Math.random(); 结果为0-1间的一个随机数(包括0,不包括1) . Math. ...

  7. Math.random()

    Math.random()*10  :  表获取  0~10  以内的随机数 一般情况 ,会对数值进行 parseInt 等取整处理 转载于:https://www.cnblogs.com/JaneB ...

  8. Math,random()返回区间内的随机数

    公式: 值 = Math.floor((Math.random() * 总数 + 开始值));   //随机1-10随机数 写成函数: function select(start,end){ var ...

  9. Java黑皮书课后题第4章:4.16(随机字符)编写一个程序,使用Math.random()方法显示一个随机的大写字母

    4.16(随机字符)编写一个程序,使用Math.random()方法显示一个随机的大写字母 题目 题目概述 破题 生成随机数 随机数与大写字母之间的转换:ASCII 转化为char型 代码 题目 题目 ...

最新文章

  1. Linux终端:speedtest_cli检测你的实时带宽速度
  2. 谈新公司的人才队伍建设
  3. Name与x:Name的关系--转载
  4. 服务器配置文件salt,saltstack 配置文件详解
  5. 面向多媒体 linux 版本,基于MX Linux 的 AV Linux 新版发布,此AV非彼AV
  6. 固定 顶部_一楼小院想建阳光房?固定的怕违建,那这样可伸缩的阳光房怎么样...
  7. Cas单点登录配置数据查询用户
  8. [Java] 蓝桥杯BASIC-18 基础练习 矩形面积交
  9. python中常量池和堆_JVM详解之:运行时常量池
  10. 毕业设计--MySqL的初探
  11. 中国城市网约车监管之政策走向与发展前景
  12. [远程桌面]程mstsc连接Windows Server2008 未安装任何音频输出设备 启用声音音频解决
  13. Windows 工作/生活 小窍门收集
  14. linux配置dns心得体会,dns实训报告心得体会.doc
  15. 蚁群算法(ant system,AS)
  16. Matplotlib多子图绘图后保存单个子图
  17. 自适应随机图片背景导航页源码
  18. 儒家、道家、佛家不同思想文化!
  19. 首创!用户级权限进程防杀 C++
  20. vs code中的platformIO插件,完成Arduino的程序编写,导入,安装开发板管理库

热门文章

  1. Java实习的记录1
  2. 【转】“Microsoft Office Word已停止工作”的终极解决方案
  3. 用十年教会自己编程_什么学习编程实际上教会了我
  4. 设置按下电源按钮时锁定计算机,WIN7定义电源按钮并启动密码保护里选项全灰...
  5. 文本框无法接受非英文字符的技术分析
  6. 查询2021职高高考成绩,2021高考什么时候出分 查成绩时间
  7. map+area标签
  8. TightVNC H264编解码(一)
  9. 网易2020校招笔试-c++开发工程师
  10. 对话阿里云崔昊:在线教育市场的变与不变?