# Coin flipping

Page 1 of 1

## 8 Replies - 312 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=bfd01394b582944cf0a16ef89a1ea89f&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);

return toss;
}
```

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;

//menu
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");
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: 5427
• Posts: 8,732
• Joined: 19-March 11

## Re: Coin flipping

Posted 10 October 2012 - 01:23 PM

What's the problem you're trying to solve here?
Was This Post Helpful? 0

### #3 pbl

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

Reputation: 8024
• Posts: 31,149
• 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());

```

Was This Post Helpful? 2

### #4 g00se

• D.I.C Lover

Reputation: 2111
• Posts: 8,793
• 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";
}
```

Was This Post Helpful? 1

### #5 CasiOo

• D.I.C Lover

Reputation: 994
• Posts: 2,204
• 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()
Was This Post Helpful? 0

### #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.
Was This Post Helpful? 0

### #7 g00se

• D.I.C Lover

Reputation: 2111
• Posts: 8,793
• 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.
Was This Post Helpful? 0

### #8 rfs02

• D.I.C Head

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.
Was This Post Helpful? 1

### #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!
Was This Post Helpful? 0

Page 1 of 1

 Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).