【问题标题】:random BigInteger array within a given range给定范围内的随机 BigInteger 数组
【发布时间】:2014-03-27 14:30:59
【问题描述】:

如何在 Java 中生成 1024 到 2048 位范围内的随机 BigInteger 数组?无需导入任何额外的外部库即可找到解决方案。

【问题讨论】:

  • 语言是什么?什么是“外部类”?
  • 在Java中,不应导入任何外部库@AlmaDo

标签: java arrays random biginteger


【解决方案1】:

此解决方案仅使用内置标准库:

import java.math.BigInteger;
import java.util.Random;

// ...

public static void main(String[] args) {
    Random randomGenerator = new Random();
    // This constructor generates a BigInteger of the number of bits given in the first argument,
    // using a random value taken from the generator passed as the second argument.
    BigInteger randomInteger = new BigInteger(1024, randomGenerator);
}

如果您想要一个难以预测的随机数,您可以选择一个安全的随机生成器:

Random randomGenerator = SecureRandom.getInstance("SHA1PRNG");

(捕获或声明NoSuchAlgorithmException

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 2014-02-25
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    相关资源
    最近更新 更多