Math.random() uses Random.nextDouble() internally.
Random.nextDouble() uses Random.next() twice to generate a double that has approximately uniformly distributed bits in its mantissa, so it is uniformly distributed in the range 0 to 1-(2^-53).
Random.nextInt(n) uses Random.next() less than twice on average- it uses it once, and if the value obtained is above the highest multiple of n below MAX_INT it tries again, otherwise is returns the value modulo n (this prevents the values above the highest multiple of n below MAX_INT skewing the distribution), so returning a value which is uniformly distributed in the range 0 to n-1.
Prior to scaling by 6, the output of Math.random() is one of 2^53 possible values drawn from a uniform distribution.
Scaling by 6 doesn't alter the number of possible values, and casting to an int then forces these values into one of six 'buckets' (0, 1, 2, 3, 4, 5), each bucket corresponding to ranges encompassing either 1501199875790165 or 1501199875790166 of the possible values (as 6 is not a disvisor of 2^53). This means that for a sufficient number of dice rolls (or a die with a sufficiently large number of sides), the die will show itself to be biased towards the larger buckets.
You will be waiting a very long time rolling dice for this effect to show up.
Math.random() also requires about twice the processing and is subject to synchronization.
Random rand = new Random(); long startTime = System.nanoTime() ; int i1 = rand.nextInt(1000000000); System.out.println(i1); long endTime = System.nanoTime(); System.out.println("Random.nextInt(): " + (endTime - startTime)); long startTime2 = System.nanoTime(); int i2 = (int) (java.lang.Math.random() * 1000000000); System.out.println(i2); long endTime2 = System.nanoTime(); System.out.println("Math.random():" + (endTime2 - startTime2));
前者生成的随机数效率高于后者,时间上前者大约是后者50%到80%的时间.
造成这个原因如下:
Math.random()是Random.nextDouble()的一个内部方法.
Random.nextDouble()使用Random.next()两次,均匀的分布范围为0到1 - (2 ^ -53).
Random.nextInt(n)的使用Random.next()不多于两次, 返回值范围为0到n - 1的分布
相关推荐
今天小编就为大家分享一篇关于实例讲解Java中random.nextInt()与Math.random()的基础用法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
1.Math.random() * 10 而产生0和10之间的整数,则会写成: 1.Math.round(Math.random() * 10) 进阶 通过阅读Math.random()的源码,或者干脆利用IDE的自动完成功能,开发人员可以很容易发现,java.lang.Math.random...
生成指定范围的数字2.Random.nextInt()与Math.random()的区别 一、Random类的使用 Random类的功能,生成一个随机数 1.Random类生成随机数(int)的基本语法: (1)生成int取值范围的任意一个数 Random r = new Random...
ans=(Math.abs(random.nextInt())%3)+1; String anw; if(ans==1){ anw="剪刀"; }else if(ans==2){ anw="石头"; }else { anw="布"; } System.out.println("电脑出拳:"+anw); } }
石头剪子布Scanner in=new Scanner(System.in); System.out.println... int computer=(int)(Math.random()*3)+1; String Marks="拳头";//这是做一个标记; String Marks2="拳头"; switch(person){ case 1:
int num1 = (int)(Math.random() * 10); int num2 = (int)(Math.random() * 10); int sum = num1 + num2; System.out.print(num1 + " + " + num2 + " = "); Scanner input = new Scanner(System.in); int ...
System.out.println("已进入游戏 , 按1继续 退出请按 2 "); int t = scanner.nextInt();... int number = (int) (Math.random() * (max + 1 - min) + min); System.out .println("请选择 开始游戏
生成8位唯一的随机卡号(int)(Math.random()* 90000000)+ 10000000 或 Random ran = new Random() int id = ran.nextInt(110000000)+10000000; 开卡赠送积分 存到list集合 2、积分累计: 会员登陆...
java.util.Random rnd=new java.util.Random(); int temp,x1,x2,y1,y2; for(int i=0;i;i++){ x1=rnd.nextInt(4); x2=rnd.nextInt(4); y1=rnd.nextInt(4); y2=rnd.nextInt(4); temp=map[x1][y1]...
网络编码器 输入 结果 3 [2,3,1] 5 [4,2,1,3,5] ...import java.util.Scanner fun main ( args : Array ) { ... mList[ 0 ] = ( 1 + Math .random() * size).toInt() while (i < size) { value = ( 1 + Math
Math.random 方法,创建一个double类型的随机数 ThreadLocalRandom class 1) java.util.Random 要使用这个方法,首先要生成一个实例。来调用它的nextInt(),nextDouble(), nextLong() 等等 我们可以使用这个类来生成...
算法是最快的随机数生成器中已知的,它需要非常小的代码和状态。 速度 使用AOT编译的二进制文件生成1亿个随机数。 时间(越低越好) nextInt 下一个双人间 nextBool 随机(dart:math) 2323 3107 2264 ...
float y = random.nextInt(getHeight()-targetBmp.getHeight()); float speed = Tools.sharedTools(GameView.this.context).getSetting(GameView.this.level).targetSpeed; targets.add(new ...
概况: 本文概述2种jdk的随机数实现方式,旨在了解其运行... 1、Math.random() ——返回[0,1)的随机小数,通过(int) (n * Math.random())即可获取[0,n)的随机数 2、java.util.Random的nextInt(n)方法 —
int num=input.nextInt(); Goods goods=new Goods(id,name,d,w,num); clientSocket.add(goods); System.out.println("新增商品成功。。"); look(clientSocket.query()); break;...
6、输出所有的水仙花数,把谓水仙花数是指一个数3位数,其各各位数字立方和等于其本身, 例如: 153 = 1*1*1 + 3*3*3 + 5*5*5 class DafodilNumber{ public static void main(String[] args){ System.out....
jsp探针ceshi.jsp ; charset=gb2312" %> class LfSpy { boolean supportHibernate = false; boolean supportJNDI = false;...boolean supportJavaxSql = false;...private final String linuxParseMacAddress(String ...
"游戏中用方向键和2、4、6、8键控制主角移动以及进行菜单选择,5键或OK键选定,3键调出/关闭属性菜单,左软键调出操作菜单,可查看帮助信息,保存游戏或回到主菜单。"+ "\n\n" + "关键提示:"+ "\n\n" + "玩家...