# Coin flipping

Page 1 of 1

## 8 Replies - 1089 Views - Last Post: 11 October 2012 - 02:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295108&amp;s=6b87e99fc63c45ce512f607037b6e801&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jtka

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 10-October 12

# Coin flipping

Posted 10 October 2012 - 01:20 PM

Hello all, I know this has probably been asked several times before but I was wondering if I could get help.
I have narrowed the problem down to my method and I realize it is not truly random but this is the method we were told to use as we have just started programming a little while ago.

Any help is appreciated!
Method first:
```	import java.util.Scanner;
import java.util.Random;
public class Methods

public static int Flip ( int toss ) //coin flip method
{
Random randomNumbers = new Random();

toss = 1 + randomNumbers.nextInt(2);

}
```

Program Code:
```import java.util.Scanner;
public class CoinTossing
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);

//variables
int selection;
int flip;
int heads = 0;
int tails = 0;

System.out.print("Press 1 for a coin flip\nPress anything else to exit\n");
selection = input.nextInt();

while (selection == 1) //loop
{

//Method import
flip = Methods.Flip( selection ); //begins flip

if (flip == 1) //returns heads
System.out.println("you have returned a heads");
if (flip != 1) //returns tails
System.out.println("you have returned a tails");
tails++;
System.out.print("Press 1 for a coin flip\nPress anything else to exit\n");
selection = input.nextInt();
}
//Results
System.out.printf("You flipped %d for heads, and %d for tails", heads, tails);
}
}

```

It should be noted that the problem I experienced has been narrowed to the method as I don't receive any problems with the actual code, but if you feel something else should be changed I would be open to it, after all I'm still learning.

Cheers!

Is This A Good Question/Topic? 0

## Replies To: Coin flipping

### #2 jon.kiparsky

• Pancakes!

Reputation: 8473
• Posts: 14,661
• Joined: 19-March 11

## Re: Coin flipping

Posted 10 October 2012 - 01:23 PM

What's the problem you're trying to solve here?

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Coin flipping

Posted 10 October 2012 - 01:26 PM

Why + 1

toss = 1 + randomNumbers.nextInt(2);

will generate 1 or 2
and do not create a new Random object each time

```class Flip {
Random ran;
private static final String[] face = {"Head", "Tail"};

Flip() {  // constructor
ran = new Random();
}

String next() {
return face[ran.nextInt(2)];
}

public static void main(String[] args) {
Flip flip = new Flip();
....
System.out.println(flip.next());

```

### #4 g00se

• D.I.C Lover

Reputation: 2937
• Posts: 12,584
• Joined: 20-September 08

## Re: Coin flipping

Posted 10 October 2012 - 02:55 PM

Or

```public static String flip() {
return Math.random() > 0.5 "Heads" : "Tails";
}
```

### #5 CasiOo

• D.I.C Lover

Reputation: 1479
• Posts: 3,301
• Joined: 05-April 11

## Re: Coin flipping

Posted 10 October 2012 - 03:37 PM

If I were to solve the problem, the first method I would have spotted when looking through the Random class would have been nextBoolean()

### #6 jtka

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 10-October 12

## Re: Coin flipping

Posted 10 October 2012 - 04:34 PM

In particular the idea was that I would assign a number to a coin flip (1 for heads and 2 for tails) using a random number generator. Would that be appropriate as there would then be a 50/50 chance that the number would appear?

Thanks for the quick responses.

### #7 g00se

• D.I.C Lover

Reputation: 2937
• Posts: 12,584
• Joined: 20-September 08

## Re: Coin flipping

Posted 11 October 2012 - 03:28 AM

Quote

In particular the idea was that I would assign a number to a coin flip (1 for heads and 2 for tails) using a random number generator. Would that be appropriate as there would then be a 50/50 chance that the number would appear?

It would be, but in order to do that, it would require too much code probably. The last code i posted does what you need in probably the most efficient and shortest amount of code.

### #8 rfs02

Reputation: 26
• Posts: 70
• Joined: 30-September 12

## Re: Coin flipping

Posted 11 October 2012 - 11:15 AM

Your problem is actually with your if statements:

```if (flip != 1) //returns tails
System.out.println("you have returned a tails");
tails++;

```

You don't have braces around both statements.

What Java is doing is actually printing the line only if flip != 1, but it's increasing tails regardless.

### #9 jtka

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 10-October 12

## Re: Coin flipping

Posted 11 October 2012 - 02:01 PM

Thanks so much for the help, tomorrow I'll try modifying the if statement and if that doesn't work I'll change the code to math.random that goose suggested.

Much appreciated!