Re: Needing a random number generator for scripting
Check out 'man perlfaq4', and the question titled, "How do I shuffle
an array randomly?". It gives a nice algorithm for shuffling, which
could be applied similar to below to output shuffled lines.
-Rob
> On 20010928.0900, Anthony J. Breeds-Taurima said ...
>
> On Thu, 27 Sep 2001, Joey Hess wrote:
>
> > Anthony J. Breeds-Taurima wrote:
> > > <== randomize ==>
> > > #!/usr/bin/perl
> > > ## print a file in random order
> > > while(<>) {
> > > $_{$_}=0
> > > }
> > > foreach (keys%_) {
> > > print
> > > }
> >
> > keys is hardly random. It always outputs the keys in the same order for
> > a given set of keys.
>
> I didn't say it was _good_ I just said I used it.
>
> <== randomize2 ==>
> #!/usr/bin/perl
> ## print a file in random order
> while(<>) {
> $_{$_}=rand;
> }
> foreach (sort {$_{$a} <=> $_{$b}} (keys%_)) {
> print
> }
>
> This is now basically the awk|sed|cut solution just in perl.
>
> Yours Tony.
>
> /*
> * "The significant problems we face cannot be solved at the
> * same level of thinking we were at when we created them."
> * --Albert Einstein
> */
>
>
> --
> To UNSUBSCRIBE, email to debian-user-request@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
>
>
Reply to: