Well, this should really go under "Idiotic Uses for Perl that are entirely Overkill", but alas, there is no such section...

I was inspired, if you will, by Genuine Quantum Randomness. This uses the module described there.

use HotBits; my $x = new HotBits::; my $n1 = $x->request (32); my $n2 = $x->request (32); if((unpack("i*",$n1))>(unpack("i*",$n2))){print "Heads\n";}elsif((unpa +ck("i*",$n1))==(unpack("i*",$n2))){print "The coin flew out of sight, + sorry. No answer, and you're out \$0.25.\n";}else{print "Tails\n";}

Quite minimalistic. And slow. But it works. And it's random enough, I'd say.

Update: fixed the problem that merlyn mentioned. Although the chances of the numbers being equal are kinda slim :)

Another Update: With a bit of code suggested by saucepan, I've made it a bit simpler. More effective, too. Here it is:

use HotBits; my $HotBits = new HotBits::; my $val=$HotBits->request (32); $val=unpack('%32b*', $val)%2; if($val==1){print "Heads\n";}else{print "Tails\n";}

--Psi
print(pack("h*","e4f64702566756e60236c6f637560247f602265696e676021602075627c602861636b65627e2")."\n");

Replies are listed 'Best First'.
Re: Wacky Happy Fun Genuine Coin Flipping Software OK!
by merlyn (Sage) on Jun 05, 2001 at 09:10 UTC
    I'd bet on tails, since in the case the two numbers are equal, you're printing Tails.

    Perhaps you should fetch just one bit, and use it to flip!

    -- Randal L. Schwartz, Perl hacker

      My correction is in the original now. Thanks for the heads up on that, merlyn.

      And would anyone care to elaborate on how I might do what merlyn suggests? Would that be changing up the unpack params, or what?

      --Psi
      print(pack("h*","e4f64702566756e60236c6f637560247f602265696e676021602075627c602861636b65627e2")."\n");

        print "The coin flew out of sight, sorry. No answer, and you're out $0 +.25.\n";

        Seeing as you're using double quotes, won't the $0 be interpolated? I doubt this is what you intended, so you might want to consider escaping the $ or using single quotes and concatinating the newline onto the string.

        Ohh and in reply to "Well, this should really go under "Idiotic Uses for Perl that are entirely Overkill", but alas, there is no such section..." there is in fact such a section. ;)
        Well... (completely untested)
        use HotBits; my $x = new HotBits::; my $n1 = $x->request (1); ($n1 & 1) ? print "Heads\n" : print "Tails\n";
        Okay, I lied and just tested it, and it's giving me all heads. Maybe i'm just being lucky =-) (it's entirely more likely that I'm just being buggy)

        ~Cybercosis nemo accipere quod non merere

        Changing the unpack param to b* gives you a bit string. Unpack won't give you a list directly.

        my @array= map { qw(heads tails)[$_] } (split (//, unpack ("b*", $bits +))); print "@array";
        —John
Re: Wacky Happy Fun Genuine Coin Flipping Software OK!
by John M. Dlugosz (Monsignor) on Jun 06, 2001 at 00:07 UTC
    "Idiotic Uses for Perl that are entirely Overkill" If they had such a section, I'd use it :).

    Although the chances of the numbers being equal are kinda slim What, merlyn doesn't like it when a coin lands on its edge?

    BTW, the request() is in =bytes=, not bits. unpack "i*" would return a list.

    So, request some hotbits, and unpack into binary, then print heads/tails for each bit. Do I hear Golf...? —John