a method that returns equal chance of head or tail

Discussion in 'Java' started by strutsng@gmail.com, Oct 7, 2005.

  1. Guest

    I need to write a flip method that returns equal chance of head or
    tail. Do you think a test for x % 2 is "equal chance"? Well, random
    number generator never gets result "equal chance." Is that correct?

    Here's my attempts, please advise.

    public String flip()
    { Random r = new Random();
    int x = r.nextInt();
    if (x % 2 == 0)
    { numHeads++;
    return "H";
    }
    numTails++;
    return "T";
    }

    Thanks!!
     
    , Oct 7, 2005
    #1
    1. Advertising

  2. E11 Guest

    Depends on how precisely "equal chance" you want. If you want truly
    absolutely equal chance (i.e. out of 1,000,000 invocations, you get
    exactly 500,000 heads and 500,000 tails), then you can do this:

    int i = 0;
    public String flip()
    {
    i++;
    if ((i % 2) == 0)
    {
    numHeads++;
    return "H";
    }
    else
    {
    numTails++;
    return "T";
    }
    }

    Otherwise, your method using the RNG should do fine. BTW, its better to
    use the same instance of Random throughout, i.e.:

    Random r = new Random();
    public String flip()
    {
    int x = r.nextInt();

    if (x % 2 == 0)
    {
    numHeads++;
    return "H";
    }

    numTails++;
    return "T";
    }



    Regards,
    Edwin
     
    E11, Oct 7, 2005
    #2
    1. Advertising

  3. Roedy Green Guest

    On 6 Oct 2005 21:45:15 -0700, wrote or quoted :

    >I need to write a flip method that returns equal chance of head or
    >tail. Do you think a test for x % 2 is "equal chance"? Well, random
    >number generator never gets result "equal chance." Is that correct?


    see http://mindprod.com/jgloss/gotchas.html#RANDOM
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 7, 2005
    #3
  4. writes:

    > I need to write a flip method that returns equal chance of head or
    > tail. Do you think a test for x % 2 is "equal chance"? Well, random
    > number generator never gets result "equal chance." Is that correct?
    >
    > Here's my attempts, please advise.
    >
    > public String flip()
    > { Random r = new Random();
    > int x = r.nextInt();
    > if (x % 2 == 0)


    Learn about java.util.Random instead, and for heads and tails use its
    nextBoolean() method. As the other guy said, move the generator out of
    the method; only initialize it once. Now you are really just sampling
    the current time, which is not a random generator at all.
     
    Jussi Piitulainen, Oct 7, 2005
    #4
  5. Googmeister Guest

    wrote:
    > I need to write a flip method that returns equal chance of head or
    > tail.


    Use Random.nextBoolean or (Math.random() < 0.5).
     
    Googmeister, Oct 8, 2005
    #5
  6. Roedy Green Guest

    On 8 Oct 2005 03:16:50 -0700, "Googmeister" <>
    wrote or quoted :

    >Use Random.nextBoolean or (Math.random() < 0.5).


    Stick with nextBoolean unless you are using an early JVM that does
    not support it. Beware: there are other ways of doing it that give
    biased results.
    .. See http://mindprod.com/jgloss/gotchas.html#RANDOM
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Oct 8, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian W
    Replies:
    10
    Views:
    782
    Brian W
    Jul 2, 2003
  2. showme

    PHP and ASP.NET go HEAD to HEAD

    showme, Jul 8, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    735
    Sherif ElMetainy
    Jul 10, 2004
  3. Camellia
    Replies:
    8
    Views:
    356
    Camellia
    Oct 7, 2006
  4. Trans

    head/tail split for path

    Trans, Jul 26, 2008, in forum: Ruby
    Replies:
    12
    Views:
    227
    ara.t.howard
    Jul 28, 2008
  5. Terry Michaels

    Tail Call Optimization (Tail Recursion)

    Terry Michaels, Apr 18, 2011, in forum: Ruby
    Replies:
    16
    Views:
    315
    Robert Klemme
    Apr 20, 2011
Loading...

Share This Page