Search code examples
javastringvariablesint

Having trouble with initializing a variable with two different options of random numbers


I'm attempting to create a MasterCard number generator (shady, I know, but it's just for class I promise! :) ) The card number has to start with either a number between 51-55 (inclusive) or a number between 222100-272099 (also inclusive). Is there any way to set a variable to be either of these? This is the line of code I wrote, but it returned this error message: "error: bad operand types for binary operator '||' ".

int firstNum = randGen.nextInt(5) + 51 || randGen.nextInt(50000) + 222100;

This number will end up eventually being a string 16 numbers long that follows the Luhn formula, so if there is an easier way to do this/troubleshoot it, please let me know!


Solution

  • You can set a variable to either of those values, but only one at a time. You need to make a choice. You can make the choice in random using the random number generator you already have.

    if (randGen.nextBoolean())
        firstNum = randGen.nextInt(5) + 51;
    else 
        firstNum = randGen.nextInt(50000) + 222100;