8 Replies - 708 Views - Last Post: 11 October 2012 - 02:01 PM Rate Topic: -----

#1 jtka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7561
  • View blog
  • Posts: 12,679
  • 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  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • 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  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1370
  • View blog
  • Posts: 3,021
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2639
  • View blog
  • Posts: 11,148
  • 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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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