生成指定位数的随机数?

huangapple go评论61阅读模式
英文:

Generating a random number with specified bit length?

问题

我正在尝试创建一个函数,该函数可以生成一个随机数,并指定随机数的位数。

更具体地说,我正在尝试生成一个可能是 8、10 或 12 位的数字。这个位数由另一个变量指定。

然后,我想将这个数字表示为十六进制字符串,或者表示为有符号和无符号的十进制整数。我认为我可以在这部分使用各种 Integer.toString 方法。

如何在 Java 中生成特定位数的随机数呢?

到目前为止,我已经有了以下函数框架:

public static String generateQuestion(int numBits, int taskType){
    String questionStr = "";
    String questionVal = ""; // 这是随机生成的数字的十六进制字符串版本
    // TODO: 基于 numBits 生成随机值的代码

    
    switch(taskType){
        case 0: // 十六进制转十进制
            questionStr = "对于 " + numBits + "-位值 " + questionVal + ",有符号和无符号值分别是什么?";
            return questionStr; // 返回构造的字符串
        case 1: // 有符号十进制转十六进制
        case 2: // 无符号十进制转十六进制
        default:
            throw new IllegalStateException("意外的问题类型:" + taskType); // 如果任务类型不是三种可能的问题类型之一,则抛出异常
    }
}
英文:

I'm trying to make a function that can generate a random number and specify how many bits the random number can be.

More specifically, I am trying to generate a number that could either be 8, 10, or 12 bits long. This bit amount is specified by another variable.

I then want to represent this number as a hex in string or as an signed and unsigned integer in decimal. I think i can just use the various Integer.toString methods for that part.

How would I go about generating specific bit-length random numbers in Java?

So far, I have this function skeleton in place:

public static String generateQuestion(int numBits, int taskType){
    String questionStr = "";
    String questionVal = ""; // This is the string hexadecimal version of the randomly generated number
    // TODO: Code to generate random value based on numBits
    

    switch(taskType){
        case 0: // Hex to Decimal
            questionStr = "What are the signed and unsigned values for the " + numBits + "-bit value " + questionVal + "?";
            return questionStr; // Return the constructed string
        case 1: // Signed Decimal to Hex
        case 2: // Unsigned Decimal to Hex
        default:
            throw new IllegalStateException("Unexpected Question Type: " + taskType); // In case the task type is something other than the 3 possible question types
    }
}

答案1

得分: 5

创建一个新的java.util.Random实例,最好是整个应用程序只有一个实例。

然后,您可以在其上调用.nextInt(x),其中x是上限(不包括在内)。因此,如果您想要一个4位的随机数,您将调用.nextInt(16),或者如果您觉得更可读(它会编译成完全相同的字节码),可以使用.nextInt(0x10),它会生成一个介于0(包括)和16(不包括)之间的随机数,因此从00001111

要找到数字'16',只需使用1 << (bits-1)。例如,rnd.nextInt(1 << (12-1))将给您一个12位的随机数。

数字只是数字。int类型没有十六进制、十进制或二进制的概念。这是您在打印时选择的内容。例如,String.format("%x", 10)将打印出A

英文:

Make a new instance of java.util.Random, preferably only one for your entire app.

Then, you can invoke .nextInt(x) on it, where x is the upper bound (exclusive). So, if you want a 4 bit random number, you'd call .nextInt(16), or if you find it more readable (it turns into the exact same bytecode), .nextInt(0x10), which generates a random number between 0 (inclusive) and 16 (exclusive), so, from 0000 to 1111.

To find the '16', it's just 1 &lt;&lt; (bits-1). e.g. rnd.nextInt(1 &lt;&lt; (12-1)) will give you a 12 bit random number.

Numbers just are. An int has no notion of hex or decimal or binary. That's a thing you choose when you print it. String.format(&quot;%x&quot;, 10) will print A for example.

huangapple
  • 本文由 发表于 2020年10月16日 09:55:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64381907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定