1

I'm creating a "roulette wheel" for a programming class assignment. This wheel generates random numbers from 0 to 36 using Math.random. One part of the assignment is to add "double zero" (00) as a possible output. Is it possible to do so through this equation?

     spin = (int) (Math.random()*(37 - 0)) + 0;
kkfonix
  • 11
  • 1
  • 2
    Because `00` is equivalent to `0` in Java, you'll need to generate an extra number and consider it to be `00`. – Jacob G. Oct 29 '19 at 22:35
  • Not sure to fully understand your goal. Is "00" supposed to be different from "0" ? In fact, could you list all possible outcoming values ? Which "real world" use case does it match ? – Michael Zilbermann Oct 29 '19 at 22:47
  • are figures from 0 to 9 supposed to be displayed with a leading 0? If so, this question might be a duplicate of [How can I pad an integer with zeros on the left?](https://stackoverflow.com/questions/473282/how-can-i-pad-an-integer-with-zeros-on-the-left) – jhamon Oct 29 '19 at 22:57
  • Possible duplicate of [How can I pad an integer with zeros on the left?](https://stackoverflow.com/questions/473282/how-can-i-pad-an-integer-with-zeros-on-the-left) – jhamon Oct 29 '19 at 22:58

3 Answers3

1

Assuming you want 00 and 0 to be a separate output, you will need to get a String instead, as integers treat the two as the same value. An option I thought of is to use a 39th possible output. You could add this in your code below:

String getSpin() {
    int spin = (int)(Math.random() * 38);
    if (spin == 38) return "00";
    else return Integer.toString(spin);
}
MAO3J1m0Op
  • 423
  • 3
  • 14
0

When you want to print 00 you should take 0 convert it to string and add "0" to it and print it as 00 and in the application logic use only one 0 and make the app give it double change of hitting it

SECI
  • 103
  • 2
  • 10
0

"00" can be NOT integer in Java so we can use String type for "00". I thought we can prepare String array contains numbers as string from 00 to 36, then generate random number from 0 to 37 because length of string array is 38. And then get a value from array at position for the random number.

I coded and put it in this answer, hope it can help you. Cheers your assignment.

public static void main(String[] args) {
    // Generate numbers as String from 00 to 36
    String numbers[] = new String[38];
    numbers[0] = "00";
    for(int i=1; i<numbers.length; i++) {
        numbers[i] = Integer.toString(i-1);
    }

    // Generate random number
    int min = 0;
    int max = numbers.length - 1;
    int randomNum = min + (int)(Math.random() * ((max - min) + 1));

    // Return number at a position of randomNum
    System.out.println("Output: " + numbers[randomNum]);
}
YUKI
  • 6,274
  • 1
  • 7
  • 8