Results 1 to 4 of 4
Thread: Math.random not zero
- 02-28-2010, 01:21 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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.
- 02-28-2010, 01:31 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
(int)(rand.nextInt()*100)%99)+1
I think that will work...
- 02-28-2010, 01:44 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 8
- Rep Power
- 0
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.
- 02-28-2010, 01:57 AM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Similar Threads
-
Math.random
By p0rnstar in forum New To JavaReplies: 9Last Post: 01-27-2010, 01:26 AM -
Math.random()
By Dieter in forum New To JavaReplies: 4Last Post: 09-14-2009, 09:28 AM -
[SOLVED] Help with math.random
By tomiu in forum New To JavaReplies: 10Last Post: 12-21-2008, 09:55 PM -
Math.Random
By Java Tip in forum Java TipReplies: 0Last Post: 11-23-2007, 02:09 PM -
math.random function help
By katie in forum New To JavaReplies: 2Last Post: 08-06-2007, 03:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks