• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Coin toss  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
The problem goes like this: Make the program throw the coin 100 times. Then sum the 1s and 2s. I succesfully made the throwing part with Math.random and for, but how can I sum the output 1s and 2s?
 
Aron Gaspar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:What constitutes a '1' or a '2'?


1 for heads and 2 for tails
 
Marshal
Posts: 13439
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Random has a better API, especially for this kind of problem. Check out the nextBoolean method. If you use that, it's a simple if-else-statement to add to heads total or tails total.

In fact, you don't even need an else part since you know how many trials you have in all.
 
Aron Gaspar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Can you post the code you have so far?


sure
math-random.png
[Thumbnail for math-random.png]
 
Junilu Lacar
Marshal
Posts: 13439
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post code as images. UseCodeTags instead.
 
Junilu Lacar
Marshal
Posts: 13439
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idiom for iterating 100 times in Java is


Try to write idiomatic code so other people won't have to do a double take on your code.
 
Aron Gaspar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Please don't post code as images. UseCodeTags instead.


 
Aron Gaspar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The idiom for iterating 100 times in Java is


Try to write idiomatic code so other people won't have to do a double take on your code.


ah okay, ill try this one quickly
 
Junilu Lacar
Marshal
Posts: 13439
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a smaller challenge that might help you get closer to a solution:

Write a private method isHeads() that returns true if your "coin toss" produces heads, false otherwise.

You can then use that method to write clearer code that solves your original problem.
 
Sheriff
Posts: 5917
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aron Gaspar:

To make your posts better (and therefore easier to get answers) be sure to create a meaningful subject line.  "Java problem" tells us nothing about what the problem is.  I've changed it for you this time.
 
Marshal
Posts: 64088
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like to see arithmetic with Math#random() to get an int. See this discussion. If you have a binary choice, either one thing or a second, consider Random#nextBoolean() instead. Somebody hasalready suggested a method with a boolean return type.
 
Campbell Ritchie
Marshal
Posts: 64088
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The idiom for iterating 100 times in Java is . . .

There is another reason for that idiom: it always works on arrays, etc., without throwing an out of bounds Exception.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!