Knuth shuffle: Difference between revisions
Content added Content deleted
m (→{{header|Inform 6}}: Match style to the DM4) |
No edit summary |
||
Line 4,714: | Line 4,714: | ||
<pre>1 2 3 4 5 6 7 8 9 10 -> |
<pre>1 2 3 4 5 6 7 8 9 10 -> |
||
2 10 4 9 1 5 6 8 7 3</pre> |
2 10 4 9 1 5 6 8 7 3</pre> |
||
=={{header|SparForte}}== |
|||
As a structured script. |
|||
<syntaxhighlight lang="ada">#!/usr/local/bin/spar |
|||
pragma annotate( summary, "shuffle" ); |
|||
pragma annotate( description, "Implement the Knuth shuffle (aka the" ); |
|||
pragma annotate( description, "Fisher-Yates-Durstenfeld shuffle)" ); |
|||
pragma annotate( description, "for an integer array (or, if possible, an array of any" ); |
|||
pragma annotate( description, "type). The Knuth shuffle is used to create a random" ); |
|||
pragma annotate( description, "permutation of an array." ); |
|||
pragma annotate( description, "Note: spar has a built-in arrays.shuffle() function that does this." ); |
|||
pragma annotate( see_also, "http://rosettacode.org/wiki/Knuth_shuffle" ); |
|||
pragma annotate( author, "Ken O. Burtch" ); |
|||
pragma license( unrestricted ); |
|||
pragma restriction( no_external_commands ); |
|||
procedure shuffle is |
|||
subtype array_element_type is string; |
|||
type magic_items is array(1..3) of array_element_type; |
|||
a : magic_items := ( "bell", "book", "candle" ); |
|||
t : array_element_type; |
|||
k : integer; |
|||
begin |
|||
for i in reverse arrays.first( a ) .. arrays.last( a )-1 loop |
|||
k := integer( numerics.rnd( i+1 ) ) - 1 + arrays.first(a); |
|||
t := a(i); |
|||
a(i) := a(k); |
|||
a(k) := t; |
|||
end loop; |
|||
for i in arrays.first( a ) .. arrays.last( a ) loop |
|||
? a(i); |
|||
end loop; |
|||
end shuffle;</syntaxhighlight> |
|||
=={{header|Stata}}== |
=={{header|Stata}}== |