• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Having trouble with Math.random

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
grapes are vegan food pellets. Eat this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic