The other day, I was sitting in my physics lab (radiation detection) and my professor said that our next lab was to flip a coin 200 times. He then said: "If you have any programming skills, you can do it with a program." You should have seen the smile on my face when I heard that. So, sitting in my apartment that night, while talking to my girlfriend, I had a revelation.

perl -e "for(1..200) {print ['H','T']->[rand(2)]}"
Note: This was done under Win32 with ActivePerl 522 (5005_03), which is the reason for the double quotes. Any more golfing welcome...

Theodore Charles III
Network Administrator
Los Angeles Senior High
4650 W. Olympic Blvd.
Los Angeles, CA 90019
323-937-3210 ext. 224
email->secon_kun@hotmail.com

Replies are listed 'Best First'.
Re: New golf game (for the extremely bored)
by chipmunk (Parson) on Feb 08, 2002 at 17:49 UTC
    Two solutions of 38 characters:
    # 1 2 3 #2345678901234567890123456789012345678 perl -le'print+(H,T)[rand 2]for a..gr' perl -le'print 1<rand 2?H:T for a..gr'
      Using your second example, you can take the score down to 37 like this:

      perl -le'print+rand>.5?H:T for a..gr'

      --Dave

      Can someone explain that "a..gr" thing to me please?

      I'm baffled.
      --

      ($_='
        jjjjjjuuuuuuuuusssssssssttttttttt annnnnnoootttttthhhhheeeeeerrrrrr
        pppeeeerrrrrrrrrrrrllllllllllllll haaaaccccccckkkkkkkeeeeeeeerrrrrr')
                     =~y/[a-z]//s;print;
        >Can someone explain that "a..gr" thing to me please?

        Same as 1..200, really ;)

        Basically you just use letters instead of numbers.

        a..z yields the 26 characters of the alphabet,
        a..az yields those and another 26 prefixed with a
        a..bz yields those and another 26 prefixed with b
        and so on. Same system, more "numbers" ...

        Incidentally a..gr yields 7*26 + 18 "numbers".

        update : as crazyinsomniac pointed out, the range-operator is discussed in more detail in perlman:perlop.
Re: New golf game (for the extremely bored)
by jmcnamara (Monsignor) on Feb 08, 2002 at 17:07 UTC

    #23456789_123456789_123456789_123456789 (39) perl -le'print+(H,T)[rand 2]for+1..200'

    --
    John.

Re: New golf game (for the extremely bored)
by japhy (Canon) on Feb 08, 2002 at 17:14 UTC
    This one works, but it requires you send it 201 lines of input. perl -pe'$_=(H,T)[rand 2],201..die'

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: New golf game (for the extremely bored)
by broquaint (Abbot) on Feb 08, 2002 at 17:36 UTC
    Variation on a theme
    # 1 2 3 4234567 perl -le'print qw(h t)[rand>.5?0:1]for 1..200'
    Mmmm, ternary conditional operatery goodness ...
    Potted at 47.

    broquaint

Re: New golf game (for the extremely bored)
by jynx (Priest) on Feb 08, 2002 at 20:09 UTC

    it's not a best entry, but here's another way to do it:
    #23456789_123456789_123456789_123456789_123456789_ perl -e'print chr(rand>.5?84:72)for-1..198'
    jynx
Re: New golf game (for the extremely bored)
by Necos (Friar) on Feb 08, 2002 at 17:33 UTC
    Using John's trick, you can do:
    perl -e "print+(H,T)[rand(2)]for(1..200)"
    Theodore Charles III
    Network Administrator
    Los Angeles Senior High
    4650 W. Olympic Blvd.
    Los Angeles, CA 90019
    323-937-3210 ext. 224
    email->secon_kun@hotmail.com
Re: New golf game (for the extremely bored)
by vek (Prior) on Feb 08, 2002 at 17:05 UTC
    Of course you meant to write:
    perl -e "for(1..200) {print ['H', 'T']->[rand(2)];}"
    didn't you ;)
      Nope. Since the for() loop is only one line, it'll work without the semicolon. As a matter of fact, I could have easily have done...
      perl -e "print ['H','T']->[rand(2)] for(1..200)"
      Theodore Charles III
      Network Administrator
      Los Angeles Senior High
      4650 W. Olympic Blvd.
      Los Angeles, CA 90019
      323-937-3210 ext. 224
      email->secon_kun@hotmail.com
        Ha ha, Necos++ you're right of course, I mistyped it when I tried to run it. Coupled with the fact that I've just spent the last 3 hours debugging a co-workers program that had missing semi-colons (I've got missing semi-colons on the brain!!)
Re: New golf game (for the extremely bored)
by Anonymous Monk on Feb 11, 2002 at 19:32 UTC
    2 36 char ways, but I cheated... you get 0's and 1's instead of H's and T's
    perl -e'map{printf"%.0f",rand}a..gr'
    perl -e'map{printf"%d",rand 2}a..gr'
Re: New golf game (for the extremely bored)
by Anonymous Monk on Feb 11, 2002 at 21:27 UTC
    Two nearly-identical (just swapped print) 36-char long ones.. 'cept these print H & T unlike the above 2

    perl -e'map{print rand>.5?H:T}a..gr'
    perl -e'print map{rand>.5?H:T}a..gr'
Re: New golf game (for the extremely bored)
by Anonymous Monk on Feb 11, 2002 at 21:31 UTC
    Actually I was thinking that the 'x' operator would be the shortest way to do this... i was trying stuff like:

    perl -e'print+(H,T)rand 2x200'

    'cept the rand doesn't run every 'x', only the first time, so I either get 200 H's or 200 T's. is it possible to evaluate the rand on each concatenation?