Generate random numbers without repeating a value: Difference between revisions
Content added Content deleted
Catskill549 (talk | contribs) (added AWK) |
|||
Line 72: | Line 72: | ||
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> |
</pre> |
||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f GENERATE_RANDOM_NUMBERS_WITHOUT_REPEATING_A_VALUE.AWK |
|||
BEGIN { |
|||
limit = 20 |
|||
srand() |
|||
printf("range 1-%d:",limit) |
|||
while (count < limit) { |
|||
n = sprintf("%d",int(rand()*limit)+1) |
|||
if (!(n in arr)) { |
|||
printf(" %d",n) |
|||
arr[n] = "" |
|||
count++ |
|||
} |
|||
} |
|||
printf("\n") |
|||
exit(0) |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
range 1-20: 16 18 15 4 13 6 11 2 1 20 14 3 7 19 17 12 10 9 5 8 |
|||
</pre> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |