简体   繁体   English

在Java中生成随机整数列表(以类似Scala的方式)

[英]Generating a list of random Integers in Java (in a Scala-like fashion)

Scala中是否存在Java 8或更高版本中的等效内容?

Seq.fill(n)(Random.nextInt)

You can use Random.ints(n) to generate n random numbers. 您可以使用Random.ints(n)生成n随机数。 To avoid boxing, you probably want to put them into an array: 为了避免装箱,你可能想把它们放到一个数组中:

int[] seq = rnd.ints(n).toArray();

If you want a list and you don't mind boxing, you can do this: 如果你想要一个列表并且你不介意拳击,你可以这样做:

List<Integer> list = rnd.ints(n).boxed().collect(toList());

The pseudorandom number generator defined by java.util.Random is used if rnd is an instance of that class. 如果rnd是该类的实例,则使用java.util.Random定义的伪随机数生成器。 This should be fine for most purposes. 对于大多数用途,这应该没问题。 If you want to use a different source of randomness, you can override Random.next(bits) to supply bits from a custom source instead of from the built-in generator. 如果要使用不同的随机源,可以覆盖Random.next(bits)以从自定义源而不是内置生成器提供位。 For example, you can do this: 例如,您可以这样做:

WARNING: THIS IS A TERRIBLE SOURCE OF PSEUDORANDOM NUMBERS. 警告:这是伪造号码的可怕来源。 DON'T USE IT. 不要使用它。 THIS IS JUST AN EXAMPLE. 这只是一个例子。

class MyRandom extends Random {
    int state = 0;
    protected int next(int bits) {
        return state ^= 1;
    }
}

Random rnd = new MyRandom();
int[] seq = rnd.ints(n).toArray();

This lets you use your own source of random bits using the API of java.util.Random , such as the ability to get values of various primitives, in different distributions, and streams of different types, ranges, and lengths. 这使您可以使用java.util.Random的API来使用自己的随机位源,例如能够获取不同分布中的各种基元的值,以及不同类型,范围和长度的流。

Maybe somewhat closer approximation than Random.ints() would be something like this: 也许有点接近Random.ints()近似值是这样的:

Stream
  .generate(rnd::nextInt)
  .limit(n)
  .collect(toList());

It is "closer" in the sense that, if one ignores minor syntactic differences and Seq vs. List distinction, both the Scala version and this version unify with a function of signature 它是“更接近”的意思是,如果一个人忽略了较小的句法差异和Seq vs. List区别,那么Scala版本和这个版本都会与签名功能统一起来

(Int, Unit => T) => Seq[T]

for any T : 任何T

(n, f) => Stream.generate(f).limit(n).collect(toList())
(n, f) => Seq.fill(n)(f)

Unlike Random.ints() , this version works for more complex distributions, and it actually limits the length to some number n , just like the Scala version does. Random.ints()不同,此版本适用于更复杂的发行版,它实际上将长度限制为某个数字n ,就像Scala版本一样。 Depending on what you want to do with it, you can collect it to some other data structure, or just leave it as Stream . 根据您要对其执行的操作,您可以collectcollect到其他数据结构中,或者将其保留为Stream


Full demo with all imports: 所有导入的完整演示:

import java.util.stream.*;
import java.util.Random;
import java.util.List;
import static java.util.stream.Collectors.toList;

public class RandomListGeneration {
    public static void main(String args[]) {
        Random rnd = new Random();
        int n = 42;
        List<Integer> result = Stream
            .generate(rnd::nextInt)
            .limit(n)
            .collect(toList());
        System.out.println(result);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM