File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

random percentages

 
Bob Beerbower
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Philip Heller
author
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can save some cycles by not casting to int and not adding 1:

 
Bob Beerbower
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic