Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: A Random Fortune Cookie

by Your Mother (Archbishop)
on Mar 10, 2021 at 16:41 UTC ( [id://11129410]=note: print w/replies, xml ) Need Help??


in reply to A Random Fortune Cookie

This is a rather old, and clever, merlyn (IIRC) recipe.

$/ = "\n\n"; rand($.) < 1 && ($line = $_) while <DATA>; print $line; __DATA__ You cannot achieve the impossible without attempting the absurd. Thinking you know something is a sure way to blind yourself. The first principle is that you must not fool yourself -- and you are +the easiest person to fool. -- Richard Feynman

Replies are listed 'Best First'.
Re^2: A Random Fortune Cookie
by choroba (Cardinal) on Mar 10, 2021 at 21:46 UTC
    It's a special case of the Fisher-Yates shuffle, IIRC.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      AFAIK does Fisher-Yates start with the highest range in rand not the lowest.

      The proof may contain similar elements but I have trouble seeing the obvious analogy...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re^2: A Random Fortune Cookie (updated)
by LanX (Saint) on Mar 10, 2021 at 17:12 UTC
    Since $. is the number of the record, I'd guess this is a biased choice because only the first is guaranteed to be <1.

    Or do I miss a math trick to even the choices?

    update

    Actually it should work fine.

    You have to think it from the end, the n'th line has a 1/n chance which is correct.

    The line before is competing with the rest and has a 1/(n-1) chance, which is correct. °

    And so on, proof by induction.

    Indeed clever! =)

    update

    °) because the lines before have a combined chance of (n-1)/n and you have to multiply the odds. This leaves line n-1 with a chance of 1/n over all. .. :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      You’re a *much* better mathematician than I. It’s from the Camel, 8.6. Picking a Random Line from a File. I can’t screenshot it or cut and paste it either because the text contains math images. The trick lies in reselecting lines as it goes and the odds of selection being “fair” across the entire file. As you say, one line is guaranteed to be picked. As subsequent lines are scanned, the odds of choosing them, and replacing the original selection, become proportionate to how many lines have been read.

      Update: didn’t see your update before I stubmitted. :P

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129410]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-23 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found