随机数⽣成器java_Java中的随机数⽣成器
随机数⽣成器 java
Today we will look at how to generate a random number in Java. Sometimes we need to generate random numbers in Java programs. For example, a dice game or to generate a random key id for encryption, etc.
今天,我们将研究如何在Java中⽣成随机数。 有时我们需要在Java程序中⽣成随机数。 例如,骰⼦游戏或⽣成⽤于加密的随机密钥ID等。Java中的随机数⽣成器 (Random Number Generator in Java)
There are many ways to generate a random number in java.
有很多⽅法可以在Java中⽣成随机数。
1. class can be used to create random numbers. It provides several methods to generate random integer, long, double
etc.
类可⽤于创建随机数。 它提供了⼏种⽣成随机整数,long,double等的⽅法。
2. We can also use to generate a double. This method internally uses Java Random class.
我们还可以使⽤⽣成⼀个double。 此⽅法在内部使⽤Java Random类。
3. urrent.ThreadLocalRandom class should be used to generate random number in multithreaded environment.
This class is part of package and introduced in Java 1.7. This class has methods similar to Java Random class.
应该使⽤urrent.ThreadLocalRandom类在多线程环境中⽣成随机数。 此类是包的⼀部分,并在Java 1.7中引⼊。 该类具有与Java Random类相似的⽅法。
4. java.security.SecureRandom can be used to generate random number with strong security. This class provides a
cryptographically strong random number generator. However, it’s slow in processing. So depending on your
application requirements, you should decide whether to use it or not.
java.security.SecureRandom可⽤于⽣成具有强⼤安全性的随机数。 此类提供了加密功能强的随机数⽣成器。 但是,它的处理速度很慢。 因此,根据您的应⽤程序要求,您应该决定是否使⽤它。
Java随机数⽣成器 (Java Random Number Generator)
Let’s look at some examples to generate a random number in Java. Later on, we will also look at ThreadLocalRandom and SecureRandom example program.
让我们看⼀些在Java中⽣成随机数的⽰例。 稍后,我们还将研究ThreadLocalRandom和SecureRandom⽰例程序。
1.⽣成随机整数 (1. Generate Random integer)
1Random random = new Random();
2
3int rand = Int();
Yes, it’s that simple to generate a random integer in java. When we create the Random instance, it g
enerates a long seed value that is used in all the nextXXX method calls. We can set this seed value in the program, however, it’s not required in most of the cases.
是的,在Java中⽣成随机整数就这么简单。 当我们创建Random实例时,它将⽣成⼀个长种⼦值,该值将在所有nextXXX⽅法调⽤中使⽤。 我们可以在程序中设置该种⼦值,但是,在⼤多数情况下,并不是必需的。
1//set the long seed value using Random constructor
2Random random = new Random(123456789);
3
4//set long seed value using setter method
5Random random1 = new Random();
6random1.setSeed(1234567890);
2. Java随机数介于1到10之间 (2. Java Random number between 1 and 10)
Sometimes we have to generate a random number between a range. For example, in a dice game possible values can be between 1 to 6 only. Below is the code showing how to generate a random number between 1 and 10 inclusive.
有时我们必须在⼀个范围之间⽣成⼀个随机数。 例如,在骰⼦游戏中,可能的值只能在1到6之间。 下⾯的代码显⽰了如何⽣成1到10之间的⼀个随机数。
1Random random = new Random();
2int rand = 0;
3while (true){
4    rand = Int(11);
5    if(rand !=0) break;
6}
7System.out.println(rand);
The argument in the nextInt(int x) is excluded, so we have to provide argument as 11. Also, 0 is included in the generated random number, so we have to keep calling nextInt method until we get a value between 1 and 10. You can extend the above code to generate the random number within any given range.
排除了nextInt(int x)的参数,因此我们必须将参数提供为11。此外,在⽣成的随机数中包含0,因此我们必须继续调⽤nextInt⽅法,直到获得介于1和10之间的值。您可以扩展以上代码以⽣成任意给定范围内的随机数。
3.⽣成随机双 (3. Generate Random double)
We can use Math.random() or Random class nextDouble method to generate random double number in java.
我们可以使⽤Math.random()或Random类的nextDouble⽅法在Java中⽣成随机双数。
1Random random = new Random();
2
3double d = Double();
4
5double d1 = Math.random();
4.产⽣随机浮动 (4. Generate Random float)
1Random random = new Random();
2
3float f = Float();
5.⽣成随机长 (5. Generate Random long)
1Random random = new Random();
2
3long l = Long();
6.⽣成随机布尔 (6. Generate Random boolean)
1Random random = new Random();
2
3boolean flag = Boolean();
7.⽣成随机字节数组 (7. Generate Random byte array)
We can generate random bytes and place them into a user-supplied byte array using Random class. The number of random bytes produced is equal to the length of the byte array.
我们可以⽣成随机字节,并使⽤Random类将其放⼊⽤户提供的字节数组中。 产⽣的随机字节数等于字节数组的长度。
1Random random = new Random();
2
3byte[] randomByteArray = new byte[5];
nextint()方法
4
6
7System.out.String(randomByteArray)); // sample output [-70, -57, 74, 99, -78]
8.多线程环境中的ThreadLocalRandom (8. ThreadLocalRandom in multithreaded environment)
Here is a simple example showing ThreadLocalRandom usage in a multithreaded environment.
这是⼀个简单的⽰例,显⽰了在多线程环境中ThreadLocalRandom的⽤法。
1package com.journaldev.randomnumber;
2
3import java.util.Random;
4import urrent.ThreadLocalRandom;
5
6public class ThreadLocalRandomExample {
7
8 public static void main(String[] args) {
9  Runnable runnable = new MyRunnable();
10
11  for (int i = 0; i < 5; i++) {
12  Thread t = new Thread(runnable);
13  t.setName("MyRunnable-Thread-" + i);
14  t.start();
15  }
16 }
17
18}
19
20class MyRunnable implements Runnable {
21
22 @Override
23 public void run() {
24  String threadName = Thread.currentThread().getName();
25  System.out.println(threadName + "::" + ThreadLocalRandom.current().nextInt());
26 }
27
28}
Below is a sample output of my execution of the above program.
以下是我执⾏上述程序的⽰例输出。
1MyRunnable-Thread-0::-1744088963
2MyRunnable-Thread-3::139405798
3MyRunnable-Thread-1::1403710182
4MyRunnable-Thread-2::-1222080205
5MyRunnable-Thread-4::-185825276
We can’t set seed value for ThreadLocalRandom instance, it will throw UnsupportedOperationException.
我们⽆法为ThreadLocalRandom实例设置种⼦值,它将引发UnsupportedOperationException 。
ThreadLocalRandom class also has some extra utility methods to generate a random number within a range. For example, to generate a random number between 1 and 10, we can do it like below.
ThreadLocalRandom类还具有⼀些额外的实⽤程序⽅法来⽣成范围内的随机数。 例如,要⽣成⼀个1到10之间的随机数,我们可以像下⾯这样进⾏操作。
1ThreadLocalRandom random = ThreadLocalRandom.current();
2
3int rand = Int(1, 11);
ThreadLocalRandom has similar methods for generating random long and double values.
ThreadLocalRandom具有类似的⽣成随机long和double值的⽅法。
9. SecureRandom⽰例 (9. SecureRandom Example)
You can use SecureRandom class to generate more secure random numbers using any of the listed providers. A quick SecureRandom example code is given below.
您可以使⽤SecureRandom类使⽤列出的任何提供程序来⽣成更多安全的随机数。 下⾯是⼀个快速的SecureRandom⽰例代码。
1Random random = new SecureRandom();
2
3int rand = Int();
4
5System.out.println(rand);
That’s all about generating a random number in Java program.
这就是在Java程序中⽣成随机数的全部内容。
. 下载⽰例代码。
随机数⽣成器 java

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。