Results 1 to 4 of 4
  1. #1
    rsvr is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default Math.random not zero

    Hi all,

    I am trying to create a initial value for a mystery win.
    When you win this mystery prize in dollars it is between the -100 and 100.
    I do not want zero as it is not achieves nothing.

    I do not want to write a long winded method for this.

    It is easy to generate a loss/win amount but I want to skip 0 dollars.

    I am doing ther following

    int rand = -101 + (int)(Math.random()*202);

    The problem is I get zero sometimes.

    The mystery win is set when the games runs and is only offered once.
    I am inplementing this in the constructor so is set once.

    How do I ignore zero without to much code in the constructor? ie. if statements.

    Thanks in advance.

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    (int)(rand.nextInt()*100)%99)+1
    I think that will work...

  3. #3
    rsvr is offline Member
    Join Date
    Dec 2009
    Posts
    8
    Rep Power
    0

    Default

    Thanks collin.

    I need to use Math.random though.
    It makes it harder if I have to create a random object and then do work on that.
    I really wanted to do it all in a couple of statements.

    Its late and I have had a few beverages. Mind cloudy and I cannt work out how to elimate zero simply.

    Thanks again.

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

Similar Threads

  1. Math.random
    By p0rnstar in forum New To Java
    Replies: 9
    Last Post: 01-27-2010, 01:26 AM
  2. Math.random()
    By Dieter in forum New To Java
    Replies: 4
    Last Post: 09-14-2009, 09:28 AM
  3. [SOLVED] Help with math.random
    By tomiu in forum New To Java
    Replies: 10
    Last Post: 12-21-2008, 09:55 PM
  4. Math.Random
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-23-2007, 02:09 PM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 03:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •