Knuth shuffle: Difference between revisions
Content added Content deleted
Line 2,182: | Line 2,182: | ||
for i = 0 to count -1 |
for i = 0 to count -1 |
||
j = rnd( i + 1 ) |
j = rnd( i + 1 ) |
||
if j > count - 1 then j = count - 1 |
|||
MutableArrayExchangeObjects( mutArr, i, j ) |
MutableArrayExchangeObjects( mutArr, i, j ) |
||
next |
next |
||
Line 2,189: | Line 2,190: | ||
CFMutableArrayRef mutArr |
CFMutableArrayRef mutArr |
||
NSUInteger i |
|||
mutArr = fn |
mutArr = fn MutableArrayWithObjects( @0, @1, @2, @3, @4, @5, @6, @7, @8, @9 ) |
||
NSLog( @"Before shuffle: %@", fn ArrayComponentsJoinedByString( mutArr, @"" ) ) |
NSLog( @"Before shuffle: %@", fn ArrayComponentsJoinedByString( mutArr, @"" ) ) |
||
for i = 1 to 10 |
|||
fn KnuthShuffle( mutArr ) |
fn KnuthShuffle( mutArr ) |
||
NSLog( @" |
NSLog( @"%@", fn ArrayComponentsJoinedByString( mutArr, @"" ) ) |
||
next |
|||
fn KnuthShuffle( mutArr ) |
|||
NSLog( @"After shuffle: %@", fn ArrayComponentsJoinedByString( mutArr, @"" ) ) |
|||
HandleEvents |
HandleEvents |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{ |
{{output}} |
||
<pre> |
<pre> |
||
Before shuffle: 0123456789 |
Before shuffle: 0123456789 |
||
1274860395 |
|||
After shuffle: 1650439827 |
|||
2715638904 |
|||
After shuffle: 6702185439 |
|||
7182035964 |
|||
1297658403 |
|||
2916574083 |
|||
9162507843 |
|||
1875962034 |
|||
8721965034 |
|||
7968402351 |
|||
9347510862 |
|||
</pre> |
</pre> |
||