Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > a method that returns equal chance of head or tail

Reply
Thread Tools

a method that returns equal chance of head or tail

 
 
strutsng@gmail.com
Guest
Posts: n/a
 
      10-07-2005
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!!

 
Reply With Quote
 
 
 
 
E11
Guest
Posts: n/a
 
      10-07-2005
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

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-07-2005
On 6 Oct 2005 21:45:15 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
Jussi Piitulainen
Guest
Posts: n/a
 
      10-07-2005
(E-Mail Removed) 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.
 
Reply With Quote
 
Googmeister
Guest
Posts: n/a
 
      10-08-2005

(E-Mail Removed) wrote:
> I need to write a flip method that returns equal chance of head or
> tail.


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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-08-2005
On 8 Oct 2005 03:16:50 -0700, "Googmeister" <(E-Mail Removed)>
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tail Call Optimization (Tail Recursion) Terry Michaels Ruby 16 04-20-2011 11:37 AM
head/tail split for path Trans Ruby 12 07-28-2008 03:27 PM
Internet Sharing: Equal upload speeds but un-equal download speeds =?Utf-8?B?TkpU?= Wireless Networking 3 09-15-2007 06:22 AM
humble coin head or tail game script I wrote Camellia Python 8 10-07-2006 09:21 AM
Cannot make head or tail of dial watcher is there anothe option to restrict hours Cisco 801 Sean Rima Cisco 3 06-24-2005 05:59 PM



Advertisments