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 ...
随机推荐
- VC创建预编译文件
Building a simple "hello world" Ogre application can take several seconds on a modern mach ...
- Bitset[HDU2051]
Bitset Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- ashx 集成于Ihandle 简易 高性能 web
不用搞MV4..如果你用.ashx写的后台..自己加一个switch case就实现了路由...更方便,还不用配置 scaffold 不是dynamic data里面的东西吗 现在我全是ash ...
- 【BZOJ】1202: [HNOI2005]狡猾的商人(并查集+前缀和)
http://www.lydsy.com/JudgeOnline/problem.php?id=1202 用并查集+前缀和. 前缀和从后向前维护和,并查集从前往后合并 对于询问l, r 如果l-1和r ...
- HDU 4671 Partition(定理题)
题目链接 这题,明显考察搜索能力...在中文版的维基百科中找到了公式. #include <cstdio> #include <cstring> #include <st ...
- hilbert
hilbert 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:131072KB: 代码长度限制:102400B 试题描述 图1为1阶Hilbert曲线,它由3条长度为1 ...
- FFmpeg - 音频解码过程
1. 注册所有解码器 av_register_all(); 2. Codec & CodecContext AVCodec* codec = avcodec_find_decoder(CODE ...
- poi读写word模板 / java生成word文档
有一word文档表格 形如: 姓名 ${name} 电话 ${tel} 从数据库读取记录替换上述变量 import java.io.FileOutputStream; import java.util ...
- MySQL 数据库设计 笔记与总结(1)需求分析
数据库设计的步骤 ① 需求分析 ② 逻辑设计 使用 ER 图对数据库进行逻辑建模 ③ 物理设计 ④ 维护优化 a. 新的需求进行建表 b. 索引优化 c. 大表拆分 [需求分析] ① 了解系统中所要存 ...
- PDO 学习与使用 ( 一 ) :PDO 对象、exec 方法、query 方法与防 SQL 注入
1.安装 PDO 数据库抽象层 PDO - PHP Data Object 扩展类库为 PHP 访问数据库定义了一个轻量级的.一致性的接口,它提供了一个数据访问抽象层,针对不同的数据库服务器使用特定的 ...