# Coin Flip Percentages

## Recommended Posts

The assignment was to write code to flip a coin 10 times and display the result as either heads or tails. Then we need to display the percentages of each result. Here is what i have so far.

public class FlipCoin{

public static void main(String[] args)

{

int loopCount = 1;

double result;

while(loopCount < 10)

{

result = Math.random();

if(result <= .5)

else

System.out.println("The result is tails.");

loopCount = loopCount + 1;

}

}

}

My guess is that this is something simple and I'm going to feel stupid once someone shows me what I'm doing wrong. I know the above code flips the coin and displays the result as either a head or a tail, all i need is to know how to then get the percentages and display them.

##### Share on other sites

Just sum up how many times you get heads, for example, and divide that figure by number of flips then multiply that by 100% (to get a value like XX%).

10 coin flips will not be enough to obtain an accurate percentage though. I altered it so the number of trials can be changed at the beginning.

```public static void main(String[] args)
{
int loopCount = 1;
int numFlips=10;
double result;

while(loopCount < numFlips){
result=Math.random();
loopCount++;
}

double percentTails = (numFlips-tails)/numFlips*100;```

Something like that. Been a while since I've done C/C++ so outputting them I'm not sure about. I forgot the syntax. I didn't count how many times tails was recorded because its simply the NON heads results. That's what the (numFlips-heads) is doing there. It's a little more efficient.

There may be errors in there but I don't have a compiler to test it.

Edited by airman

##### Share on other sites

I don't know C++, so I can't help you with the actual coding, but if the math.random() function is anything like javascript, your logic is majorly flawed.

math.random() produces results >= 0.0 and

Based on your code, this is what you're saying:

If you get the following, it's heads:

0.0

0.1

0.2

0.3

0.4

0.5

If you get the following, it's tails:

0.6

0.7

0.8

0.9

That's 6 values for heads and only 4 values for tails. As such, change "if(result

Personally though, I wouldn't do a coin flip that way...I would use integers...you roll a 1, it's heads, you roll a 2, it's tails. Like I said, I'm not familiar with C++, but in VB.net, it'd be like so:

```Dim RandomClass As New Random()
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(1,3)```

##### Share on other sites

I apologize for the confusion. The assignment is actually in Java not C++. My apologies for not specifying earlier. Also, ClayMeow, i understand what you're saying about the math.random() function. I am simply following the directions I was given. airman, thank you for your input, however, the way you have your code set up to add to the heads variable exists the if function which leaves an else with no if. I'm not sure if this is because i failed to mention that the syntax was to be Java. Other than that the code works like a charm.

##### Share on other sites

firstly, for those of you calling it C++, that code is Java.

also Math.random() returns a double value between 0.0000 and 0.9999. so doing < .5 is fine

##### Share on other sites
firstly, for those of you calling it C++, that code is Java.

also Math.random() returns a double value between 0.0000 and 0.9999. so doing

That's my point. Read his code again. He has

##### Share on other sites

no it's actually ok with the <= 5 because that only applies to .500000000000000000 nothing bigger. granted < 5 would be better, but <+ doesn't really skew the results

Edited by RimX

##### Share on other sites

```public class FlipCoin{
public static void main(String[] args)
{
int loopCount = 1;
double result;
while(loopCount < 10)
{
loopCount = loopCount + 1;
}

}
}```

You're only looping 9 times. If you start at 1 you need "loopCount <= 10" or you can start at 0 and do "loopCount < 10". I like to start at 0 but that's just preference.

To get the percentages you'll need a counter for either the heads or the tails inside the if statement for the heads or tails. No need to track both as you can do some simple math to get the other. so to get tails you could track the heads and then do:

`int tails = numberOfFlips - heads;`

assuming you tracked it in a variable called heads. Then it's just using the equation for finding a percentage.

`double percentHeads = (heads / numberOfFlips) * 100;`

and I would use a variable for the number of flips in case later your professor tells you to change it to 1000 flips. Then you just have to change one number instead of tracking down all the times you put a 10 in your code.

Edited by flareback

##### Share on other sites

Thanks to everyone who has contributed. I suck at this class. I think i understand what I need to do at this point the problem is that I seem to be unable to do it. as it stands now the code looks like this.

public class FlipCoin

{

public static void main(String[] args)

{

int loopCount = 1;

double result;

while(loopCount <= 10)

{

result = Math.random();

if(result <= .5)

if(result <= .5)

else

System.out.println("The result is tails.");

loopCount = loopCount + 1;

}

System.out.println("the percentage of tails is: " +percentTails);

}

}

I'm sure this is still wrong, because it always returns percentages of 0 and 100 for heads and tails respectively. If anyone could point out my mistake it would be a huge help. Once again, thanks to any and all who have offered input.

##### Share on other sites

first, it would help in the future if you wrapped your code in the code tags, makes it easier to read.

Now, you need to fix your if else statements. you have:

``` if(result <= .5)
if(result <= .5)
else
System.out.println("The result is tails.");```

While this would work, it has to compute whether or not result is <= 5 twice. That's redundant. Use curly braces to wrap the print statement and increment the head value in the if statement like so:

```if(result <= .5){
}```

just be sure you have your else on the end.

I don't have anything to run java right now so I can't test everything but it looks okay. I'm not sure what Math.random() returns though. if the return value is double value between 0 and 1 then you should be okay. which according to this page it does (actually it's 0 <= x < 1 where x is the returned value).

Just noticed a problem. You're doing integer division. You have an integer holding the value of heads, therefore when you divide by 10 it will always go to zero unless heads happens to be 10. You can either cast heads as a double before dividing or you can just change it from an int to a double.

That should fix it for you.

##### Share on other sites

what i suggest doing is print out the value of 'result' every time in the loop. Then you can see if the percentages make sense. If the random number generator is messing up then that might be the problem. Not sure what version of java you're using but you might have to manually initialize a seed value for the generator. It's not supposed to be like that, but hell, it's not a perfect world and i've seen stranger . happen

GL!

##### Share on other sites

Well at this point I believe i have addressed both issues that flareback pointed out. I have a second program that does almost the same exact thing and works the way it is supposed to. Only difference is that it flips the coin 1000 times as opposed to ten times and it doesnt have the print statements to display the result as either a head or a tail each time. Anyone have any idea why it works and the first program still returns 0 for heads and 100 for tails?

the code for both programs is below.

first is the program that doesnt work. Second is the program that does.

```public class FlipCoin
{
public static void main(String[] args)
{
int loopCount = 1;
double result;
while(loopCount <= 10)
{
result = Math.random();
if(result <= .5){
}
else
System.out.println("The result is tails.");
loopCount = loopCount + 1;
}
System.out.println("the percentage of tails is: " +percentTails);
}
}```

```public class FlipCoin2
{
public static void main(String[] args)
{
int loopCount = 1;
double result;
while(loopCount <= 1000)
{
result = Math.random();
if(result <= .5)
loopCount = loopCount + 1;
}
System.out.println("the percentage of tails is: " +percentTails);
}
}```