in reply to Golf: 3 hole golf game

First a random technical note. The implementation of rand guarantees that the same value cannot be returned twice in a very large number of calls. So your note about #2 and #3 is not true. If I'm free to ignore that, I get much smaller entries for those. But I'll submit entries that don't (ab)use that fact about rand.

Here is my score:

human.pl: 87 strokes shuffle.pl: 47 strokes select.pl: 52 strokes ------------------------ total: 186 strokes
Note that you might not accept my human.pl, it does have an artificial (though generous and easy to increase) limit in it. I used no modules. Here are my solutions.
human.pl ======== %x=map{($x=lc)=~s/\d+/0 x(999-length$&).$&/ge;$_,$x}@x=<>;print sort{$x{$a}cmp$x{$b}}@x shuffle.pl ========== $i=rand@x,@x[$n=@x,$i]=($x[$i],$_)for<>;print@x select.pl ========= $i=rand@x,@x[$n=@x,$i]=($x[$i],$_)for<>;print@x[0,1]

Replies are listed 'Best First'.
Re^2: Golf: 3 hole golf game
by dragonchild (Archbishop) on Nov 17, 2004 at 12:54 UTC
    Cutting 8 strokes from human.pl to 79...

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re^2: Golf: 3 hole golf game
by eric256 (Parson) on Nov 24, 2004 at 16:26 UTC

    "The implementation of rand guarantees that the same value cannot be returned twice in a very large number of calls"

    Wouldn't that be considered a bug in perl? I mean if it guarantees not to return the same number, then that number is no longer equaly likely to be picked. I had assumed that everytime rand was called every possible outcome is equaly likely. Of course I might just be a crazy lunatic.


    ___________
    Eric Hodges
      No, that is not a bug in Perl, it is a limitation that comes from fundamental facts of computer science. Perl's rand is a pseudo-random number generator. (That the documentation for rand doesn't make this clear is a bug in the documentation though...) It can't produce really random numbers (producing random numbers by a deterministic algorithm is a neat trick), but it produces something that looks pretty random for a very long time.

      If you want to produce really random numbers, then you need a source of random data to sample. Which can be done, but has its own problems (the call to fetch a random number may take a while if there is no random data in the pool right now). For normal purposes, pseudorandom is fast, easy, and good enough. Plus for some purposes it is very nice to be able to call srand and get a predictable set of numbers out.

      But if you really want something more random then see Math::TrulyRandom.