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
}
}