java的Random
首先,Point类
public class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
boolean isSame() {
return x == y;
}
}
测试代码A:
public class RandomTest {
public static Random random = new Random();
public static void main(String[] args) {
int x = 0, y = 0;
List<Point> points = new ArrayList<Point>();
for (int i = 0; i < 100000; i++) {
x = new Random().nextInt(20);
y = new Random().nextInt(20);
points.add(new Point(x, y));
}
System.out.println(getSameCount(points));
}
public static int getSameCount(List<Point> li) {
int count = 0;
for (Point point : li) {
if (point.isSame())
count++;
}
return count;
}
}
测试代码B将Main方法替换为
public static void main(String[] args) {
int x = 0, y = 0;
List<Point> points = new ArrayList<Point>();
for (int i = 0; i < 100000; i++) {
x = random.nextInt(100);
y = random.nextInt(100);
points.add(new Point(x, y));
}
System.out.println(getSameCount(points));
}
经测试
A代码打印结果在3000左右
B代码打印结果在1000左右
查了一些资料
大概是因为同时创建的两个Random对象,产生随机数的算法会比较类似,和创建对象时的时间戳有关系,所以产生的随机数相同的几率也比较大。
所以程序中应该尽量避免使用多个Random对象,或者直接使用Math.random();
欢迎 意见 建议 指正 交流。
java的Random的更多相关文章
- Java API —— Random类
1.Random类概述 此类用于产生随机数 如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列. 2.构造 ...
- java.util.Random深入理解
java.util.Random next方法的原理 比较好的参考文档: http://isky001.iteye.com/blog/1339979 package random.utilrandom ...
- [转] JAVA的Random类
Random类 (java.util) Random类中实现的随机算法是伪随机,也就是有规则的随机.在进行随机时,随机算法的起源数字称为种子数(seed),在种子数的基础上进行一定的变换,从而产生需要 ...
- java java.uitl.Random产生随机数
通过使用java.uitl.Random产生一个1-10内的随机数.例: Random random = new Random(); int i = Math.abs(random.nextInt() ...
- java.util.Random 类的 nextInt(int num )
随机产生3个67~295的整数并找出数值居中的数 并输出中间的数例如:100,225和200,输出200 要随机产生某个范围内的整数,用 java.util.Random 类的 nextInt(int ...
- Java基础-Random类(05)
随机数(Random) 作用:用于产生一个随机数 使用步骤(和Scanner类似) 导包import java.util.Random; 创建对象Random r = new Random(); 获取 ...
- 【java】彩票中奖码生成器:java.util.Random里的方法public int nextInt(int bound)
package 彩票中奖码生成器; import java.util.Random; public class TestRandom { public static void main(String[ ...
- Java的Random总结
/** * @Title:RandomNum.java * @Package:com.yhd.chart.model * @Description:Java产生随机数 * @author:Youhai ...
- java中random()函数用法介绍
Random() 创建一个新的随机数生成器. 代码如下 复制代码 Random(long seed) 使用单个 long 种子创建一个新的随机数生成器. 我们可以在构造Random对象的时候指定种子 ...
- java中random的几个方法的使用Math.random()和random().
random java中我们有时候也需要使用使用random来产生随机数,下面我来简单的介绍下java中random的使用方法 第一种:Math.random() public static doub ...
随机推荐
- POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...
- sql 关联查询
SELECT mms_sample_datas.* from mms_sample_datas where mms_sample_datas.mms_id in ( SELECT mms_sample ...
- BZOJ3532 : [Sdoi2014]Lis
f[i]表示以i为结尾的LIS长度 对于所有f[i]=1的,由S向i连边 对于所有f[i]=maxf的,由i向T连边 对于j<i,a[j]<a[i],且f[j]+1=f[i]的,j向i连边 ...
- Git 远程操作详解
Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操作非常简便.本文详细介绍5个Git命令,它们的概念和用法,理解了这些内容,你就会完全掌握Gi ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- [Unity2D]鼠标(或触摸)输入处理
在游戏的编程之中,基本上都需要依赖鼠标的输出,特别是在手机游戏上,绝大部分都需要通过手指触摸来控制游戏.如果要实现一个精灵,当手指点击精灵的会触发相关的操作,或者我们使用一张图片来作为按钮,点击的时候 ...
- 【C语言】15-预处理指令1-宏定义
预处理指令简介 1.C语言在对源程序进行编译之前,会先对一些特殊的预处理指令作解释(比如之前使用的#include文件包含指令),产生一个新的源程序(这个过程称为编译预处理),之后再进行通常的编译 2 ...
- spark Using MLLib in Scala/Java/Python
Using MLLib in ScalaFollowing code snippets can be executed in spark-shell. Binary ClassificationThe ...
- sql中写标量函数生成大写拼音首字母
USE [StockManageSystemV2] GO /****** Object: UserDefinedFunction [dbo].[PinYin] Script Date: 2016-08 ...
- Html - Bootstrap 头部
<div class="container"> <div class="row clearfix"> <div class=&qu ...