gpt4 book ai didi

java - 使用 SecureRandom 生成安全随机数

转载 作者:行者123 更新时间:2023-12-02 02:42:17 29 4
gpt4 key购买 nike

如何使用 Java 的 SecureRandom 类生成 6 位整数?

我正在尝试以下代码来生成随机数:

SecureRandom secureRandom = new SecureRandom();
int secureNumber = secureRandom.nextInt();

它生成任意长度的随机数,包括负数。我在 SecureRandom 类中没有找到任何方法来提供数字范围。我想生成一个6随机数

最佳答案

简单地用数组完成

int[] arr = new int[6];
Random rand = new SecureRandom();
for(int i= 0; i< 6 ; i++){
// 0 to 9
arr[i] = rand.nextInt(10);
}

不知道您是否需要其他类型(如果您想要 int 看这里:How to convert int array to int?

关于java - 使用 SecureRandom 生成安全随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45246479/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com