How to get a random between 1 - 100 from randDouble?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cleo Millet
    New Member
    • Oct 2011
    • 7

    How to get a random between 1 - 100 from randDouble?

    Hello,

    Okay, I'm still fairly new to Java. We've been given an assisgnment to create a game where you have to guess a random integer that the computer had generated. The problem is that our lecturer is insisting that we use:

    double randNumber = Math.random();

    And then translate that into an random integer that accepts 1 - 100 inclusive. I'm a bit at a loss. What I have so far is this:

    //Create random number 0 - 99
    double randNumber = Math.random();
    d = randNumber * 100;

    //Type cast double to int
    int randomInt = (int)d;

    However, the random the lingering problem of the random double is that 0 is a possibility while 100 is not. I want to alter that so that 0 is not a possible answer and 100 is. Help?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Add one.

    Comment

    • Cleo Millet
      New Member
      • Oct 2011
      • 7

      #3
      I'm afraid I don't understand you answer. Add 1 to what and where?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Add one to your random number after you multiply by 100.

        Comment

        • Cleo Millet
          New Member
          • Oct 2011
          • 7

          #5
          Originally posted by Rabbit
          Add one to your random number after you multiply by 100.
          Ah, thanks. That did it.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            No problem, good luck.

            Comment

            Working...