in reply to Fisher-Yates shuffle?
Luckely, there is a fix, change the rand($_-1) to rand($_+1).#!/usr/bin/perl use strict; use warnings 'all'; my %buckets; my @set = "A" .. "D"; foreach my $run (1 .. 10_000) { my @values = @set; @values[$_,$a] = @values[$a=rand($_-1),$_] for (reverse 1..$#value +s); $buckets {"@values"} ++; } foreach my $key (sort keys %buckets) { printf "%4d: %s\n" => $buckets {$key}, $key; } printf "%d buckets\n" => scalar keys %buckets; __END__ 4955: B C D A 5045: D C A B 2 buckets
Abigail
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Fisher-Yates shuffle?
by BrowserUk (Patriarch) on Aug 09, 2002 at 20:49 UTC | |
by Abigail-II (Bishop) on Aug 12, 2002 at 09:29 UTC |