JAVA小知识12:Random随机数详解

Java中的java.util.Random类用于生成伪随机数。它提供了多种方法来生成不同类型的随机数,包括整数、浮点数和布尔值。以下是对Random类及其主要方法的详细介绍

一、生成随机数 

创建一个Random对象,可以使用以下两种方式:

  • 无参构造函数:使用默认的种子(当前时间的毫秒数)

    • Random random = new Random();
  • 带参构造函数:使用指定的种子,便于生成可重复的随机数序列

    • Random random = new Random(42);

无参的构造函数非常好理解,即根据当前时间毫秒值随机生成,那么有参是什么意思呢?有参数即意味着可以生成重复序列的随机数,即如果参数一样,那么根据参数创建的两个对象所生产的随机数:第一组为1,5,7,第二组必然也为1,5,7 

二、主要方法 

 在上述创建完Random对象后,我们即可使用Random中的方法来创建伪随机数

2.1 int nextInt()

  • 返回一个随机的整数,范围是所有可能的int值。
  • int randomValue = random.nextInt();
  • 最小值:Integer.MIN_VALUE,即-2^31(-2147483648)
  • 最大值:Integer.MAX_VALUE,即2^31-1(2147483647)

2.2  int nextInt(int bound)

  • 返回一个0(包括)到bound(不包括)之间的随机整数。用法示例:
  • int randomValue = random.nextInt(10);

2.3 随机浮点数生成 double nextDouble()

  • 返回一个0.0(包括)到1.0(不包括)之间的随机浮点数。
  • double randomValue = random.nextDouble();

2.4 float nextFloat()

  • 返回一个0.0(包括)到1.0(不包括)之间的随机浮点数。
  • float randomValue = random.nextFloat();

2.5 boolean nextBoolean()

  • 返回一个随机的布尔值。
  • boolean randomValue = random.nextBoolean();

2.6 随机长整型生成long nextLong()

  • 返回一个随机的长整型值。
  • long randomValue = random.nextLong();

三、综合示例 

import java.util.Random;
import java.util.Arrays;

public class RandomExample {
    public static void main(String[] args) {
        Random random = new Random();

        // 生成一个随机整数
        int randomInt = random.nextInt();
        System.out.println("Random Integer: " + randomInt);

        // 生成一个0到9之间的随机整数
        int randomIntBounded = random.nextInt(10);
        System.out.println("Random Integer (0-9): " + randomIntBounded);

        // 生成一个随机浮点数
        double randomDouble = random.nextDouble();
        System.out.println("Random Double: " + randomDouble);

        // 生成一个随机布尔值
        boolean randomBoolean = random.nextBoolean();
        System.out.println("Random Boolean: " + randomBoolean);

        // 生成随机字节数组
        byte[] randomBytes = new byte[5];
        random.nextBytes(randomBytes);
        System.out.println("Random Bytes: " + Arrays.toString(randomBytes));

        // 生成一个随机长整型值
        long randomLong = random.nextLong();
        System.out.println("Random Long: " + randomLong);

        // 生成一个高斯分布的随机数
        double randomGaussian = random.nextGaussian();
        System.out.println("Random Gaussian: " + randomGaussian);
    }
}

 四、如何随机生成1-10中五个不重复的随机数?

 方法1:

@Test
void threadtest4() throws ExecutionException, InterruptedException {
    //生成一个数组
    int[] arr = new int[5];
    int count=0;
    Random random = new Random();
    while (count<5){
        Boolean isBreak=true;
        //生成的随机数
        int i = random.nextInt(5) + 1;
        //判断是否重复
        for(int j=0; j<arr.length; j++){
            if(arr[j]==i){
                isBreak=false;
            }
        }
        //不重复就进入数组中
        if(isBreak) {
            arr[count] = i;
            count++;
        }
    }
    Arrays.stream(arr).forEach(e-> System.out.println(e));
}

 其结果:

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值