aspose file tools*
The moose likes Beginning Java and the fly likes random percentages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "random percentages" Watch "random percentages" New topic
Author

random percentages

Bob Beerbower
Ranch Hand

Joined: Jun 06, 2001
Posts: 32
I'm trying to come up with some code that will run a given pecentage of the time. In other words I want say a 15% chance that a certian branch will be taken. This is what I have come up with so far, but I'm wondering is there a cleaner solution.

[ June 06, 2005: Message edited by: Bob Beerbower ]

Regards Bob Beerbower
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Looks like something I've done in other languages, so I'd call it a fine way to go. I'd think about the variable name ... it holds a random number between 1 and 100 ... is fifteenPercent descriptive?


If you have a 15% chance of branch A and a 30% chance of B you can elaborate like this:


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
You can save some cycles by not casting to int and not adding 1:



Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Bob Beerbower
Ranch Hand

Joined: Jun 06, 2001
Posts: 32
Originally posted by Philip Heller:
You can save some cycles by not casting to int and not adding 1:



Perfect, just what I was looking for. I new I was over complicating this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: random percentages
 
Similar Threads
Precedence
For Loop Problem
Encrypting XML file
Calculating hashcode in double hashing
function for IP address to integer conversion