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!
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

Greenhorn
Posts: 6
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?

Bartender
Posts: 5652
56
What constitutes a '1' or a '2'?

Aron Gaspar
Greenhorn
Posts: 6

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

1 for heads and 2 for tails

Carey Brown
Bartender
Posts: 5652
56
Can you post the code you have so far?

Sheriff
Posts: 13183
219
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

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

sure
math-random.png

Junilu Lacar
Sheriff
Posts: 13183
219

Junilu Lacar
Sheriff
Posts: 13183
219
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

Aron Gaspar
Greenhorn
Posts: 6

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
Here's a smaller challenge that might help you get closer to a solution:

You can then use that method to write clearer code that solves your original problem.

Sheriff
Posts: 5747
149
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
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

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/