in reply to Re^2: random #s
in thread random #s
Since you have numbers, use the "<=>" operator instead.
ALso, please use <code/> tags.
Output:use strict; use warnings; my @array = qw(1 4 6 7 23 45 12 1 2); while (not isSorted(@array)) { print +(join ", ", @array) . "\r"; my ($x, $y) = (rand(scalar @array), rand(scalar @array)); ($array[$x], $array[$y]) = ($array[$y], $array[$x]); } print "\nSort complete!\n"; print join ", ", @array,"\n"; sub isSorted { my $x = shift; while (my $y = shift) { return 0 unless ($x <=> $y) < 1 ; $x = $y; } return 1; }
Sort complete! 1, 1, 2, 4, 6, 7, 12, 23, 45
...it is unhealthy to remain near things that are in the process of blowing up. man page for WARP, by Larry Wall
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: random #s
by choroba (Cardinal) on Oct 07, 2016 at 08:37 UTC |