Results 1 to 7 of 7

Thread: Coin Flipper?

  1. #1
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Coin Flipper?

    I am making a coinfliper application and i dont know how to tell if it picks heads 50% of the time and tales the other 50%. My code is posted below.

    import java.util.Random;

    public class g
    {
    public static void main(String[] args)
    {
    Random generator = new Random();
    int z = generator.nextInt(9);

    if (z > 4)
    System.out.println("heads");
    if (z > 4) LoadImageApp.main(new String[0]);

    if (z < 5)
    System.out.println("tails");
    if (z < 5) ShowImage.main(new String[0]);
    }
    }

  2. #2
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: Coin Flipper?

    it seems to lean toward tails

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default Re: Coin Flipper?

    Look around the FAQs of this site and discover how to post code so that it retains its formatting.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Coin Flipper?

    It will lean towards tails.
    You are generating an int 0-8, which is 9 discrete values.
    Why not just do nextInt(2)?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jsobel is offline Member
    Join Date
    Apr 2012
    Posts
    68
    Rep Power
    0

    Default Re: Coin Flipper?

    what about now?

    import java.util.Random;

    public class g
    {
    public static void main(String[] args)
    {
    Random generator = new Random();
    int z = generator.nextInt(2);

    if (z > 0)
    System.out.println("heads");
    if (z > 0) LoadImageApp.main(new String[0]);

    if (z < 1)
    System.out.println("tails");
    if (z < 1) ShowImage.main(new String[0]);
    }
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default Re: Coin Flipper?

    Quote Originally Posted by Tolls View Post
    Why not just do nextInt(2)?
    Or nextBoolean().

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: Coin Flipper?

    Didn't realise Random had one of them...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. coin flip program
    By chandrasekhar.melam in forum New To Java
    Replies: 5
    Last Post: 09-26-2011, 05:30 PM
  2. Making a Coin Application
    By seaisle68 in forum Java Applets
    Replies: 3
    Last Post: 04-08-2011, 01:14 AM
  3. Coin assigning program
    By Jamesing in forum New To Java
    Replies: 10
    Last Post: 03-11-2011, 05:57 AM
  4. Coin application
    By kkGG in forum New To Java
    Replies: 4
    Last Post: 11-08-2010, 12:24 AM
  5. Coin Change
    By Growler in forum New To Java
    Replies: 7
    Last Post: 09-23-2010, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •