Results 1 to 9 of 9
Thread: Random coin flip application
 12172009, 11:44 PM #1Member
 Join Date
 Oct 2009
 Posts
 20
 Rep Power
 0
Random coin flip application
Application for 10 random coin flips. At the end of the application I just want to display the percentage of the heads flipped and percentage of tails flipped. Can someone point me in a right direction please?
Java Code://import statements import java.util.*; public class FlipCoin { public static void main(String [] args) { //declare variables double flip; int count = 0; int countHeads = 0; int countTails = 0; int percentHeads; int percentTails; //Loop for(int x = 0; x <= 10;x = x++) { x = x + 1; //flip flip = Math.random(); //if statement if( flip <= .5) { flip = countHeads; countHeads = countHeads + 1; percentHeads = countHeads * 10; } else { flip = countTails; countTails = countTails + 1; percentTails = countTails * 10; System.out.println("Heads was flipped " + percentHeads + " percent of the time."); System.out.println("Tails was flipped " + percentTails + " percent of the time."); } count = count +1; }//End Loop }//End Main }//End Class
Last edited by Fubarable; 12182009 at 12:03 AM. Reason: Code tags added
 12182009, 12:06 AM #2Senior Member
 Join Date
 Aug 2009
 Posts
 294
 Rep Power
 0
if you wana do this:
x = x + 1;
then do it like this instead:
x += 1;
or even better
x++;
It looks cleaner and do the same, But in this case, You shouldnt do this!
In your for loop you got at the end "x++"
This meens, it will increese the value of x with one everytime it has done one loop.
So erase the whole x = x+1; thingy.
And for the random your right now taking a random value with random amount of numbers.
Do this:
flip = Math.random(1); /this will give you a value between 0 and 1
then ask if flip is < 0.5
hope this helped
 12182009, 12:07 AM #3Senior Member
 Join Date
 Aug 2009
 Posts
 294
 Rep Power
 0
oh and put this:
Java Code:System.out.println("Heads was flipped " + percentHeads + " percent of the time."); System.out.println("Tails was flipped " + percentTails + " percent of the time.")

If you want to print something out after the loop is done, then you'll want to have your println statements after the loop closes  after your //End Loop comment.
Also, you'll likely want to give your percent variables some initial value such as 0. Also, when calculating percent, you'll first calculate frequency which will be a floating point type value (double would work best here) and then change it to percent by mult by 100 and casting to int. One pitfall  when you calculate the frequency, be sure you cast one of the numbers used in this equation (usually I use the numerator) to double when you divide. Otherwise you'll be doing int division: an int / int will return an int always  and you don't want to do this. For instance 1/ 2 returns 0 (that's the double result rounded down to an int), but (double)1/2 returns 0.5.
Edit: also please learn to use code tags as it makes your code much easier to read. I added them to your original post, but you should be doing this yourself. Please see my signature below to learn how to do this.
 12182009, 02:39 AM #5Member
 Join Date
 Oct 2009
 Posts
 20
 Rep Power
 0
I see exactly what both of you were talking about. I will certainly learn to use code tags. Thanks for all your help, I appreciate it.
 12182009, 03:18 AM #6Member
 Join Date
 Oct 2009
 Posts
 20
 Rep Power
 0
One more quick question, sorry...but how would I get the result to stop printing 20 times. What would I do if I wanted to just print the percentages of heads and tails?
Thank you

Did you take our advice? Is your call to System.out.println inside the for loop or outside of it?
 12182009, 03:56 AM #8Member
 Join Date
 Oct 2009
 Posts
 20
 Rep Power
 0
It is outside it???
 12182009, 03:57 AM #9Member
 Join Date
 Oct 2009
 Posts
 20
 Rep Power
 0
Similar Threads

Java Coin Acceptor?
By starzsimon in forum Advanced JavaReplies: 5Last Post: 08062011, 08:50 AM 
how i use the random class to random the cards i have
By yanipao in forum New To JavaReplies: 14Last Post: 10192009, 11:57 AM 
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04192009, 06:50 PM 
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01172008, 11:44 PM 
Problem calling classes to flip coin x number of times and record heads or tails
By adlb1300 in forum New To JavaReplies: 2Last Post: 11112007, 09:07 AM
Bookmarks