Java – Random Long, Float, Integer and Double – Java – 随机的Long, Float, Integer and Double

最后修改: 2014年 7月 20日

This quick tutorial will illustrate how to generate a long first using plain Java and using the Apache Commons Math library.

这个快速教程将说明如何使用普通的Java和使用Apache Commons数学库来生成一个长的第一。

This article is part of the “Java – Back to Basic” series here on Baeldung.

本文是Baeldung网站上“Java – Back to Basic “系列的一部分。

1. Generate an Unbounded Long

1.产生一个无界的长条

Let’s start with generating a Long:

让我们从生成一个Long开始。

@Test
public void givenUsingPlainJava_whenGeneratingRandomLongUnbounded_thenCorrect() {
    long generatedLong = new Random().nextLong();
}

2. Generate a Long Within a Range

2.在一个范围内产生一个长条

2.1. Random Long With Plain Java

2.1.使用普通Java的随机长

Next – let’s look at creating a random bounded Long – that is, a Long value within a given range or interval:

接下来–让我们看看如何创建一个随机的有界龙–即在一个给定的范围或区间内的龙值。

@Test
public void givenUsingPlainJava_whenGeneratingRandomLongBounded_thenCorrect() {
    long leftLimit = 1L;
    long rightLimit = 10L;
    long generatedLong = leftLimit + (long) (Math.random() * (rightLimit - leftLimit));
}

2.2. Random Long With Apache Commons Math

2.2.使用Apache Commons数学的随机长

Let’s take a look at generating the random Long with a cleaner API and Commons Math:

让我们来看看用更干净的API和Commons Math生成随机Long的情况。

@Test
public void givenUsingApacheCommons_whenGeneratingRandomLongBounded_thenCorrect() {
    long leftLimit = 10L;
    long rightLimit = 100L;
    long generatedLong = new RandomDataGenerator().nextLong(leftLimit, rightLimit);
}

3. Generate an Unbounded Integer

3.生成一个无界的整数

Let’s move right on to generating a random Integer with no bounds:

让我们继续生成一个无边界的随机整数。

@Test
public void givenUsingPlainJava_whenGeneratingRandomIntegerUnbounded_thenCorrect() {
    int generatedInteger = new Random().nextInt();
}

As you can see, it’s pretty close to generating a long.

正如你所看到的,它非常接近于生成一个长。

4. Generate an Integer Within a Range

4.在一个范围内生成一个整数

4.1. Random Integer With Plain Java

4.1.使用普通Java的随机整数

Next – a random integer within a given range:

下一个 – 在给定范围内的一个随机整数。

@Test
public void givenUsingPlainJava_whenGeneratingRandomIntegerBounded_thenCorrect() {
    int leftLimit = 1;
    int rightLimit = 10;
    int generatedInteger = leftLimit + (int) (new Random().nextFloat() * (rightLimit - leftLimit));
}

4.2. Random Integer With Commons Math

4.2.使用Commons Math的随机整数

And the same with Common Math:

而共同数学也是如此。

@Test
public void givenUsingApache_whenGeneratingRandomIntegerBounded_thenCorrect() {
    int leftLimit = 1;
    int rightLimit = 10;
    int generatedInteger = new RandomDataGenerator().nextInt(leftLimit, rightLimit);
}

5. Generate an Unbounded Float

5.生成一个无界的浮点数

Now, let’s go over generating random floats – first unbounded:

现在,让我们来看看生成随机浮点数的情况–首先是无界的。

@Test
public void givenUsingPlainJava_whenGeneratingRandomFloatUnbouned_thenCorrect() {
    float generatedFloat = new Random().nextFloat();
}

6. Generate a Float Within a Range

6.在一个范围内生成一个浮点

6.1. Random Float With Plain Java

6.1.使用普通Java的随机浮点运算

And a bounded random float:

还有一个有界的随机浮子。

@Test
public void givenUsingPlainJava_whenGeneratingRandomFloatBouned_thenCorrect() {
    float leftLimit = 1F;
    float rightLimit = 10F;
    float generatedFloat = leftLimit + new Random().nextFloat() * (rightLimit - leftLimit);
}

6.2. Random Float With Commons Math

6.2.使用Commons数学的随机浮点运算

Now – a bounded random float with Commons Math:

现在–一个有界的随机浮子与Commons数学。

@Test
public void givenUsingApache_whenGeneratingRandomFloatBounded_thenCorrect() {
    float leftLimit = 1F;
    float rightLimit = 10F;
    float randomFloat = new RandomDataGenerator().getRandomGenerator().nextFloat();
    float generatedFloat = leftLimit + randomFloat * (rightLimit - leftLimit);
}

7. Generate an Unbounded Double

7.生成一个无界的双数

7.1. Random Unbounded Double With Plain Java

7.1.使用普通Java的随机无界双胞胎

Finally – we’re going to generate random double values – first, with the Java Math API:

最后–我们要生成随机的双倍值–首先,用Java Math API。

@Test
public void givenUsingPlainJava_whenGeneratingRandomDoubleUnbounded_thenCorrect() {
    double generatedDouble = Math.random();
}

7.2. Random Unbounded Double With Commons Math

7.2.随机无界的双数与共享数学

As well as a random double value with the Apache Commons Math library:

以及用Apache Commons数学库的随机双倍值。

@Test
public void givenUsingApache_whenGeneratingRandomDoubleUnbounded_thenCorrect() {
    double generatedDouble = new RandomDataGenerator().getRandomGenerator().nextDouble();
}

8. Generate a Double Within a Range

8.在一个范围内生成一个双数

8.1. Random Bounded Double With Plain Java

8.1.使用普通Java的随机有界双胞胎

In this example, let’s take a look at a random double generated within an interval – with Java:

在这个例子中,我们来看看在一个区间内生成的随机双倍数–用Java。

@Test
public void givenUsingPlainJava_whenGeneratingRandomDoubleBounded_thenCorrect() {
    double leftLimit = 1D;
    double rightLimit = 10D;
    double generatedDouble = leftLimit + new Random().nextDouble() * (rightLimit - leftLimit);
}

8.2. Random Bounded Double With Commons Math

8.2.随机有边界的双倍数与共享数学

And lastly – a random double within an interval, using the Apache Commons Math library:

最后–使用Apache Commons数学库,在一个区间内随机生成一个双数。

@Test
public void givenUsingApache_whenGeneratingRandomDoubleBounded_thenCorrect() {
    double leftLimit = 1D;
    double rightLimit = 100D;
    double generatedDouble = new RandomDataGenerator().nextUniform(leftLimit, rightLimit);
}

And there you have it – quick and to the point examples of how to generate both unbounded and bounded values for the most common numerical primitives in Java.

这就是你所拥有的–关于如何为Java中最常见的数字基元生成无界值和有界值的快速而有针对性的例子。

9. Conclusion

9.结语

This tutorial illustrated how we could generate random numbers either bound or unbound, using different techniques and libraries.

这个教程说明了我们如何使用不同的技术和库来生成绑定或未绑定的随机数。

As always, the implementation of all of these examples and snippets can be found in the GitHub project. This is a Maven-based project so it should be easy to import and run.

一如既往,所有这些例子和片段的实现都可以在GitHub项目中找到。这是一个基于Maven的项目,所以它应该很容易导入和运行。