Here is the code I have so far. I'm not sure how to proceed from here. My goal is to make a program that outputs the probability of n amount of coin tosses, where n is the user input.

import java.util.Scanner;
public class toss{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter the number of flips: ");
		int n = scanner.nextInt();
		double x = 0;
		for(int i=1; i<n; i++)
		{
			if(Math.random() < 0.5)
			{
				x++;
			}
			System.out.println("P(H) = "+(double)x/n);
		}
	}
}

Again, I'm not looking for the code, just some advice on where to fix and what next. My knowledge is limited to for loops, ifs, whiles, and just the beginning of Strings.
Thanks in advance!

program that outputs the probability of n amount of coin tosses

What does your program output now?
If you want it to output something different, please explain and show what you want.

At line 8, if you want to loop n time, you should start your variable from 0 to n-1 as

for (int i=0; i<n; i++)

The way you do right now is from 1 to n-1.

For your variable "x" you should use "int" instead of "double" and then cast it to double when you do the result display.

Also, take your line 14 out of the loop (swap it with the "}" in line 15). Currently, you are computing the probability over and over in the for-loop.

I want it to output the probability of X amount of tosses. For example, if the user inputs 100 (for the amount of coin tosses), then it will toss the coin 100 times, and output the percentage of each in decimal value.


Will try to set i = 0.

If you move the output outside the loop as I said, you should get the probability which is 0<= probability <=1 because the value of x will be between 0 and n exclusive (0<=x<=n). The format decimal for display, however, could be adjusted by using DecimalFormat class if I remember correctly.

What if i wanted to make it print only the probability of heads or tails, and not ALL of the tries?

i mean of the total amount of tries, the end results of what you get for all of the tries.

i mean of the total amount of tries, the end results of what you get for all of the tries.

Move the print out of the loop.

import java.util.Scanner;
public class toss{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter the number of flips: ");
		int n = scanner.nextInt();
		int x = 0;
		for(int i=0; i<n; i++)
		{
			if(Math.random() < 0.5)
			{
				x++;
			}
		}
		System.out.println("P(H) = "+(double)x/n);
	}
}

This is the code so far. So if i wanted to make it not only print the probability of heads for x number of tosses, but also to print tails too.

would it work if i made an else if statement? what should i set the boolean?

print ... also to print tails too.

What is in the variable x and the variable n?
What is their relationship?

I believe that n is the number of tosses that the user inputs.
and the x is heads?

import java.util.Scanner;
public class toss{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter the number of flips: ");
		int n = scanner.nextInt();
		int x = 0;
		int y = 0;
		for(int i=0; i<n; i++)
		{
			if(Math.random() < 0.5)
			{
				x++;
			}
			else if(Math.random() > 0.5)
			{
				y++;
			}
		}
		System.out.println("P(H) = "+(double)x/n);
		System.out.println("P(T) = "+(double)y/n);
	}
}

This is my attempt. The results are definitely false, so i'm trying to figure out the incorrect part of the algorithm for tails.

Because there are only 2 different values you want to collect and the total of the 2 is equal to n, you can calculate one if you know the other. That's why else statement does nothing good but make the code look busier in the code.

In your code, however, make a mistake because it doesn't count anything. You redo the random again and that's wrong. the y value is actually around 1/4 of probability...

Get rid of the "else if" statement and update line 21 to be System.out.println("P(T) = "+double(n-x)/n);

Assume that it is head whenever random number < 0.5 is

I see. This is the end results, and the program works perfectly.

import java.util.Scanner;
public class toss{
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		System.out.print("Enter the number of flips: ");
		int n = scanner.nextInt();
		double x = 0;
		for(int i=0; i<n; i++)
		{
			double ran = Math.random();
			if(ran < 0.5)
			{
				x++;
			}
		}
		System.out.println("P(H) = "+x/n);
		System.out.println("P(T) = "+(n-x)/n);
	}
}

Thank you everyone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.