java随机数_Java随机「建议收藏」

java随机数_Java随机「建议收藏」java随机数JavaRandomclassisusedtogenerateaseriesofrandomnumbers.JavaRandom类用于生成一系列随机数。Java随机类(JavaRandomClass)Randomclassispartofjava.utilpackage.Random类是java.util包的一部分。Anins…

大家好,又见面了,我是你们的朋友全栈君。

java随机数

Java Random class is used to generate a series of random numbers.

Java Random类用于生成一系列随机数。

Java随机类 (Java Random Class)

  • Random class is part of java.util package.

    Random类是java.util包的一部分。

  • An instance of java Random class is used to generate random numbers.

    java Random类的一个实例用于生成随机数。

  • This class provides several methods to generate random numbers of type integer, double, long, float etc.

    此类提供了几种生成整数,双精度,长整型,浮点型等随机数的方法。

  • Random number generation algorithm works on the seed value. If not provided, seed value is created from system nano time.

    随机数生成算法对种子值起作用。 如果未提供,则从系统纳米时间创建种子值。

  • If two Random instances have same seed value, then they will generate same sequence of random numbers.

    如果两个随机实例具有相同的种子值,则它们将生成相同的随机数序列。

  • Java Random class is thread-safe, however in multithreaded environment it’s advised to use java.util.concurrent.ThreadLocalRandom class.

    Java Random类是线程安全的,但是在多线程环境中,建议使用java.util.concurrent.ThreadLocalRandom类。

  • Random class instances are not suitable for security sensitive applications, better to use java.security.SecureRandom in those cases.

    随机类实例不适用于对安全敏感的应用程序,在这种情况下最好使用java.security.SecureRandom

Java随机构造函数 (Java Random Constructors)

Java Random class has two constructors which are given below:

Java Random类具有两个构造函数,如下所示:

  1. Random(): creates new random generator

    Random() :创建新的随机数生成器

  2. Random(long seed): creates new random generator using specified seed

    Random(long seed) :使用指定的种子创建新的随机生成器

Java随机类方法 (Java Random Class Methods)

Let’s have a look at some of the methods of java Random class.

让我们看一下Java Random类的一些方法。

  1. nextBoolean(): This method returns next pseudorandom which is a boolean value from random number generator sequence.

    nextBoolean() :此方法返回下一个伪随机数,它是随机数生成器序列中的布尔值。

  2. nextDouble(): This method returns next pseudorandom which is double value between 0.0 and 1.0.

    nextDouble() :此方法返回下一个伪随机数,该伪随机数是0.0到1.0之间的双精度值。

  3. nextFloat(): This method returns next pseudorandom which is float value between 0.0 and 1.0.

    nextFloat() :此方法返回下一个伪随机数,该伪随机数是介于0.0和1.0之间的float值。

  4. nextInt(): This method returns next int value from random number generator sequence.

    nextInt() :此方法从随机数生成器序列返回下一个int值。

  5. nextInt(int n): This method return a pseudorandom which is int value between 0 and specified value from random number generator sequence.

    nextInt(int n):此方法返回一个伪随机数,它是介于0和随机数生成器序列中指定值之间的int值。

Java随机示例 (Java Random Example)

Let’s have a look at the below java Random example program.

让我们看一下下面的Java Random示例程序。

package com.journaldev.examples;

import java.util.Random;

/**
 * Java Random Number Example Program
 * 
 */
public class RandomNumberExample {

	public static void main(String[] args) {
		
		//initialize random number generator
		Random random = new Random();
		
		//generates boolean value
		System.out.println(random.nextBoolean());
		
		//generates double value
		System.out.println(random.nextDouble());
		
		//generates float value
		System.out.println(random.nextFloat());
		
		//generates int value
		System.out.println(random.nextInt());
		
		//generates int value within specific limit
		System.out.println(random.nextInt(20));

	}

}

Output of the above program is:

上面程序的输出是:

false
0.30986869120562854
0.6210066
-1348425743
18

Check this post for more about Java Radom Number Generation.

查看这篇文章,了解有关Java Radom Number Generation的更多信息。

使用种子生成随机数 (Generate Random Number using Seed)

There are two ways we can generate random number using seed.

我们可以通过两种方式使用种子生成随机数。

Random random = new Random(long seed);
                         
Random random1 = new Random();
random1.setSeed(seed);

The seed is the initial value of the internal state of the pseudorandom number generator which is maintained by method next(int).

种子是通过方法next(int)维护的伪随机数生成器内部状态的初始值。

package com.journaldev.examples;

import java.util.Random;

/**
 * Java Random Number Using Seed Example Program
 * 
 * @author pankaj
 *
 */
public class RadomSeedExample {

	public static void main(String[] args) {
		
		//using constructor
		Random random = new Random(100);
		System.out.println("Using Constructor");
		System.out.println(random.nextInt());
		
		//using method
		Random random2 = new Random();
		random2.setSeed(200);
		System.out.println("Using Method");
		System.out.println(random2.nextInt());
	}
}

Output of the above program is:

上面程序的输出是:

Using Constructor
-1193959466
Using Method
-1133938638

What if we pass same seed to two different random number generators?

如果我们将相同的种子传递给两个不同的随机数生成器怎么办?

Let’s have a look at the below program and see what happen if we pass same seed to two different random number generators.

让我们看一下下面的程序,看看如果将相同的种子传递给两个不同的随机数生成器,会发生什么情况。

package com.journaldev.examples;

import java.util.Random;

/**
 * Java Random Number Using Same Seed Example Program
 * 
 */
public class RandomNumberSameSeedExample {

	public static void main(String[] args) {
		
		//initialize two random number generators using same seed
		Random random1 = new Random(100);
		Random random2 = new Random(100);
		
		System.out.println(random1.nextInt());
		System.out.println(random2.nextInt());

	}

}

Output of the above program is:

上面程序的输出是:

-1193959466
-1193959466

We can see that it will generate same random number if we pass same seed to two different random number generators.

我们可以看到,如果我们将相同的种子传递给两个不同的随机数生成器,它将生成相同的随机数。

Java 8随机类方法 (Java 8 Random Class Methods)

As you can see from above image, there are many new methods added in Java 8 to Random class. These methods can produce a stream of random numbers. Below is a simple program to generate a stream of 5 integers between 1 and 100.

从上图可以看到,Java 8中为Random类添加了许多新方法。 这些方法可以产生随机数流。 下面是一个简单的程序,用于生成1到100之间的5个整数的流。

package com.journaldev.random;

import java.util.Random;
import java.util.stream.IntStream;

public class JavaRandomExample {

	public static void main(String[] args) {

		Random random = new Random();

		// generate stream of 5 ints between 1 to 100
		IntStream ints = random.ints(5, 1, 100);

		ints.forEach(System.out::println);
	}

}

That’s all for a quick roundup on Java Random Class.

这就是Java随机类的快速汇总。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/17111/java-random

java随机数

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/156635.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号