This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Coin toss  RSS feed

 
Greenhorn
Posts: 6
  • 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: 6
  • 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
 
Sheriff
Posts: 13183
219
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: 6
  • 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
Sheriff
Posts: 13183
219
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
Sheriff
Posts: 13183
219
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: 6
  • 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: 6
  • 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
Sheriff
Posts: 13183
219
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: 5747
149
Chrome Eclipse IDE Java Postgres Database 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: 63489
207
  • 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: 63489
207
  • 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.
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!