Generate random numbers without repeating a value: Difference between revisions
Content added Content deleted
(Added Algol 68) |
|||
Line 99: | Line 99: | ||
14 2 1 18 10 7 13 11 17 4 20 9 6 3 16 19 5 15 12 8 |
14 2 1 18 10 7 13 11 17 4 20 9 6 3 16 19 5 15 12 8 |
||
</pre> |
|||
=={{header|ALGOL 68}}== |
|||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
|||
{{libheader|ALGOL 68-rows}} |
|||
This is vertually identical to the Algol 68 sample for the Knuth Shuffle Task. |
|||
<lang algol68># generate a set of 20 random integers without duplicates # |
|||
# same as the Knuth Shuffle sample, but with different display # |
|||
PR read "rows.incl.a68" PR # include array related utilities # |
|||
PROC between = (INT a, b)INT : |
|||
( |
|||
ENTIER (random * ABS (b-a+1) + (a<b|a|b)) |
|||
); |
|||
PROC knuth shuffle = (REF[]INT a)VOID: |
|||
( |
|||
FOR i FROM LWB a TO UPB a DO |
|||
INT j = between(LWB a, UPB a); |
|||
INT t = a[i]; |
|||
a[i] := a[j]; |
|||
a[j] := t |
|||
OD |
|||
); |
|||
main:( |
|||
[20]INT a; |
|||
FOR i FROM 1 TO 20 DO a[i] := i OD; |
|||
knuth shuffle(a); |
|||
SHOW a |
|||
)</lang> |
|||
{{out}} |
|||
<pre> |
|||
17 6 11 5 7 15 18 8 4 3 10 13 9 2 12 1 19 14 20 16 |
|||
</pre> |
</pre> |
||