File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Having trouble with Math.random Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having trouble with Math.random" Watch "Having trouble with Math.random" New topic

Having trouble with Math.random

Sally Curtis

Joined: Jan 10, 2004
Posts: 26
hi i need to write a program that collects bottle caps. this program is supposed to collect all 5 bottle caps with the letters BASIC. Each letter has a ratio of being found...B-20% A-20% S-40% I-19% C-1%.....
i want to use math.random...the range of it i want is 1-100...i made if statements..the first 20 is b, next 20 is A, next 40 is S next 19 is I and the last one is C.... the program is supposed to print out the number of bottle caps needed to get all 5 to win a prise and the number of each kind. now the following code compiles, but when i run it it does not work.
would i rather need a random class?
pls help
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Hmmm... so what does the program do when it "does not work"? Print an error message? Print anything at all? You can always add some print statements for debugging purposes to figure out what's going on. E.g.

By studying the output of statements like this, you can get more information about what your program is actually doing. Is it what you expect? How is it different? When you get to a part of the program which behavaes differently than you expect, you can add additional print statements to get more info than I've provided so far. If the results still don't make sense to you, post here describing what sort of results you're getting. Hope that helps...

"I'm not back." - Bill Harding, Twister
David Crossett
Ranch Hand

Joined: Feb 05, 2003
Posts: 102
Without looking too deeply at your code, the first thing I see is that your loop will only run once. The reason is that - according to your 'if' conditional, the loop only runs AS LONG AS ALL letters == 0. Once you increment a letter ( which should be done B += 1 ), you make this statement false, so the loop stops executing. Does that help?

David Crossett
-nothing important to say, but learnin' plenty-
I agree. Here's the link:
subject: Having trouble with Math.random
It's not a secret anymore!