在Java中生成随机数的快速有效方法 [英] Quick and Efficient way to generate random numbers in Java

查看:203
本文介绍了在Java中生成随机数的快速有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个生成大量随机数的多线程Java程序。



其他详细信息:
这些数字用于创建0-99的随机数列表而不重复,并且列表中存在0-99范围内的每个数字(换句话说,该列表包含0-99范围内的100个唯一元素)。 / p>

生成随机数[事情已经尝试过!]


  1. 我有一个0-100的数字列表。我生成一个随机数,并将其用作索引,用于从 ArrayList 中弹出一个元素。

  2. 我用过 Collections.shuffle()

以下是方法1的代码:

  ArrayList< Integer> arr = new ArrayList< Integer>(); 
for(int i = 0; i< N; i ++){
arr.add(i,i);
}

for(int i = 0; i< N; i ++){
int indx = rand.nextInt(arr.size());
res.add(arr.get(indx));
arr.remove(indx);
}

对于第二种方法,我替换了第二种 for 循环使用 Collections.shuffle(arr)



由于生成随机数列表是我算法中最昂贵的部分,我想优化它。这让我想到了这些问题:


  1. 生成随机数的最快方法是什么?

  2. 如上所述生成随机数列表的最快方法是什么?

PS:


  1. 我发现 Collections.shuffle()要慢于第一种方法

  2. 有人建议我使用 rngd 从Unix中的硬件生成随机数。有没有人试过这个?你是怎么做到的?


解决方案

我认为 Collections.shuffle( ) 使用默认 随机 实例,它是一个线程安全的单例。你说你的程序是多线程的,所以我可以想象 Random 中的同步是一个瓶颈。



如果您正在使用Java 7,请使用 ThreadLocalRandom 。仔细看,有一个 shuffle() 明确地采用 Random 实例:

  Collections.shuffle(arr,threadLocalRandom); 

其中 threadLocalRandom 只创建一次。



在Java 6上,您只需为每个线程创建一个 Random 的实例。请注意,每次运行时不应创建 Random 的新实例,除非您每次都可以提供随机种子。


I am writing a multi-threaded Java program that generates lot of random numbers.

Additional Details: These numbers are used to create a list of random numbers from 0-99 without repetition and such that every number in the range 0-99 exists in the list (In other words, the list contains 100 unique elements in the range 0-99).

Generating Random Numbers [Things already tried!]

  1. I have an ArrayList of numbers from 0-100. I generate a random number and use it as an index which is used to pop out an element from the ArrayList.
  2. I have used Collections.shuffle().

Here is the code for approach 1:

ArrayList<Integer> arr = new ArrayList<Integer>(); 
for (int i = 0; i < N; i++){
 arr.add(i, i);
}

for(int i=0; i<N; i++){
  int indx = rand.nextInt(arr.size());
  res.add(arr.get(indx));
  arr.remove(indx);
}

For second approach, I replaced the second for loop with Collections.shuffle(arr).

As generating list of random numbers is the most expensive part of my algorithm, I want to optimize it. This brings me to the questions:

  1. What is the fastest way to generate random numbers?
  2. What is the fastest way to generate the list of random numbers as described above?

PS:

  1. I found Collections.shuffle() to be slower than the first approach
  2. Someone suggested me using rngd to generate random numbers from hardware in Unix. Has anyone tried this before? How do you do that?

解决方案

I think the problem with Collections.shuffle() is that is uses default Random instance which is a thread-safe singleton. You say that your program is multi-threaded, so I can imagine synchronization in Random being a bottle-neck.

If you are happily running on Java 7, simply use ThreadLocalRandom. Look carefully, there is a version of shuffle() taking Random instance explicitly:

Collections.shuffle(arr, threadLocalRandom);

where threadLocalRandom is created only once.

On Java 6 you can simply create a single instance of Random once per thread. Note that you shouldn't create a new instance of Random per run, unless you can provide random seed every time.

这篇关于在Java中生成随机数的快速有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆