生成随机数的Java程序

112 阅读3分钟

用一个例子写一个生成随机数的Java程序。例如,在Java中,我们可以使用Math随机方法、类和ThreadLocalRandom类来生成数字。

使用Math函数生成随机数的Java程序

在这个Java例子中,我们使用Math.random()方法,它可以生成0和1之间的双倍类型的数字。我们还可以对输出进行类型转换以获得必要的数据类型。

package RemainingSimplePrograms;

public class Example {
	
	public static void main(String[] args) {
	
		System.out.println("The First  = " + Math.random());
		
		System.out.println("The Second = " + Math.random());
		
		System.out.println("The Third  = " + Math.random());
		
		System.out.println("The Fourth = " + Math.random());
	}
}
The First  = 0.20037158212760142
The Second = 0.0012068894419960952
The Third  = 0.18700555955501663
The Fourth = 0.3230234616758232

通过使用Java的Math.random() * (max - min + 1) + min,我们可以生成min和max值之间的数字。

package RemainingSimplePrograms;

public class Example2 {


public static void main(String\[\] args) {
	
	int min = 100, max = 300;

	System.out.print("The Doubles between " + min + " and " + max  + " = " );
	double rand1 = Math.random() \* (max - min + 1) + min;
	System.out.print(rand1);
	
	System.out.print("\\nThe Number between " + min + " and " + max  + " = " );
	int rand2 = (int)(Math.random() \* (max - min + 1) + min);
	System.out.print(rand2);
}
The Doubles between 100 and 300 = 101.43411590322778
The Number between 100 and 300 = 282

到目前为止,我们在一个范围之间生成了一个单一的数值。然而,在for循环的帮助下,我们可以在一个范围内生成它们。

public class Example {


public static void main(String\[\] args) {
	int min = 10, max = 100;
	
	System.out.println("Ten Numbers between " + min + " and " + max);
	for(int i = 1; i <= 10; i++)
	{
		int rdNum = (int)(Math.random() \* (max - min + 1) + min);
		System.out.print(rdNum + "   ");
	}
	
	System.out.println("\\nTwenty Numbers between 100 and 250");
	for(int i = 1; i <= 10; i++)
	{
		int rdNum = (int)(Math.random() \* (250 - 100 + 1) + 100);
		System.out.print(rdNum + "   ");
	}
}

使用类来生成随机数的Java程序

这个Java随机类有不同的方法来生成数字。它们是:

  • nextInt() - 产生0和-1之间的值
  • nextInt(max_Value)
  • nextFloat() - 返回0.0和1.0之间的值
  • nextDouble() - 返回0.0和1.0之间的数值
  • nextLong()
  • nextBoolean()
package RemainingSimplePrograms;

import java.util.Random;

public class Example4 {

public static void main(String\[\] args) {
	
	Random rand = new Random();
	
	System.out.println("Integer Values");
	System.out.println(rand.nextInt());
	System.out.println(rand.nextInt(20));
	System.out.println(rand.nextInt(100));
		
	System.out.println("Double Values");
	System.out.println(rand.nextDouble());
	System.out.println(rand.nextDouble());
	
	System.out.println("Float Values");
	System.out.println(rand.nextFloat());
	System.out.println(rand.nextFloat());
	
	System.out.println("Long Values");
	System.out.println(rand.nextLong());
	System.out.println(rand.nextLong());
}
Integer Values
1907114976
4
34
Double Values
0.9425130257739933
0.35445514266974565
Float Values
0.26128042
0.47151804
Long Values
1983140345491940719
8269807721314480176

在这个Java例子中,我们用for循环来生成十个最大极限值为100的Integer类型的随机数。

package RemainingSimplePrograms;

import java.util.Random;

public class Example5 {

public static void main(String\[\] args) {
	
	Random rand = new Random();

	for(int i = 1; i <= 10; i++)
	{
		System.out.print(rand.nextInt(100) + 1 + "   ");
	}
}
9   45   62   84   18   2   10   33   28   60 

使用ThreadLocalRandom

我们还可以使用ThreadLocalRandom来生成一个范围内的随机数。nextInt()、nextDouble()和nextLong()接受最小和最大极限值。

package RemainingSimplePrograms。

import java.util.concurrent.ThreadLocalRandom;

public class Example6 {

public static void main(String\[\] args) {
		
	System.out.println("Integer Values");
	System.out.println(ThreadLocalRandom.current().nextInt());
	System.out.println(ThreadLocalRandom.current().nextInt(20));
	System.out.println(ThreadLocalRandom.current().nextInt(100, 500));
		
	System.out.println("Double Values");
	System.out.println(ThreadLocalRandom.current().nextDouble());
	System.out.println(ThreadLocalRandom.current().nextDouble(50));
	System.out.println(ThreadLocalRandom.current().nextDouble(30, 50));
	
	System.out.println("Float Values");
	System.out.println(ThreadLocalRandom.current().nextFloat());
	System.out.println(ThreadLocalRandom.current().nextFloat());
	
	System.out.println("Long Values");
	System.out.println(ThreadLocalRandom.current().nextLong());
	System.out.println(ThreadLocalRandom.current().nextLong(40));
	System.out.println(ThreadLocalRandom.current().nextLong(400, 600));
}
Integer Values
536433268
17
105
Double Values
0.35695024289926547
29.875628014935078
45.29405097588917
Float Values
0.62107533
0.9211104
Long Values
-1523132321353307594
4
542

这个程序使用ThreadLocalRandom和for循环。

package RemainingSimplePrograms;

import java.util.concurrent.ThreadLocalRandom;

public class Example7 {

public static void main(String\[\] args) {
	
	int min = 100, max = 300;

	System.out.println("Ten Numbers are");
	for(int i = 1; i <= 10; i++)
	{
		System.out.print(ThreadLocalRandom.current().nextInt(100) + 1 + "   ");
	}
	
	System.out.println("\\nFifteen Numbers are");
	for(int i = 0; i < 15; i++)
	{
		int rdNum = ThreadLocalRandom.current().nextInt(min, max);
		System.out.print(rdNum + "   ");
	}
}
Ten Numbers are
10   88   47   68   41   79   45   74   1   41   
Fifteen Numbers are
262   187   267   255   255   265   136   201   228   255   166   156   115   291   169