Generate random numbers without repeating a value: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 22: | Line 22: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="11l">F generate(a, b) |
||
[Int] result |
[Int] result |
||
V count = b - a + 1 |
V count = b - a + 1 |
||
Line 36: | Line 36: | ||
L 5 |
L 5 |
||
print(generate(1, 20))</ |
print(generate(1, 20))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 48: | Line 48: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC PrintTable(BYTE ARRAY tab BYTE size) |
||
BYTE i |
BYTE i |
||
Line 86: | Line 86: | ||
PrintTable(tab,LEN) |
PrintTable(tab,LEN) |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Generate_random_numbers_without_repeating_a_value.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Generate_random_numbers_without_repeating_a_value.png Screenshot from Atari 8-bit computer] |
||
Line 105: | Line 105: | ||
{{libheader|ALGOL 68-rows}} |
{{libheader|ALGOL 68-rows}} |
||
This is vertually identical to the Algol 68 sample for the Knuth Shuffle Task. |
This is vertually identical to the Algol 68 sample for the Knuth Shuffle Task. |
||
< |
<syntaxhighlight lang="algol68"># generate a set of 20 random integers without duplicates # |
||
# same as the Knuth Shuffle sample, but with different display # |
# same as the Knuth Shuffle sample, but with different display # |
||
Line 129: | Line 129: | ||
knuth shuffle(a); |
knuth shuffle(a); |
||
SHOW a |
SHOW a |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 136: | Line 136: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">-- Return a script object containing: 1) a list of all the integers in the required range and |
||
-- 2) a handler that returns one of them at random without repeating any previous choices. |
-- 2) a handler that returns one of them at random without repeating any previous choices. |
||
-- Calls to the handler after all the numbers have been used just return 'missing value'. |
-- Calls to the handler after all the numbers have been used just return 'missing value'. |
||
Line 175: | Line 175: | ||
end task |
end task |
||
task()</ |
task()</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang="applescript">{16, 9, 12, 6, 17, 10, 1, 5, 3, 2, 7, 20, 14, 18, 19, 11, 15, 13, 8, 4}</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">generateUniqueRandoms: function [][ |
||
result: new [] |
result: new [] |
||
Line 195: | Line 195: | ||
loop 3 'x [ |
loop 3 'x [ |
||
print generateUniqueRandoms |
print generateUniqueRandoms |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 204: | Line 204: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f GENERATE_RANDOM_NUMBERS_WITHOUT_REPEATING_A_VALUE.AWK |
# syntax: GAWK -f GENERATE_RANDOM_NUMBERS_WITHOUT_REPEATING_A_VALUE.AWK |
||
BEGIN { |
BEGIN { |
||
Line 221: | Line 221: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 230: | Line 230: | ||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="basic256">arraybase 1 |
||
for num = 1 to 5 |
for num = 1 to 5 |
||
call pRand() |
call pRand() |
||
Line 252: | Line 252: | ||
until nr = 21 |
until nr = 21 |
||
print |
print |
||
end subroutine</ |
end subroutine</syntaxhighlight> |
||
==={{header|QBasic}}=== |
==={{header|QBasic}}=== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="qbasic">DECLARE SUB pRand () |
||
RANDOMIZE TIMER |
RANDOMIZE TIMER |
||
Line 279: | Line 279: | ||
LOOP UNTIL nr = 21 |
LOOP UNTIL nr = 21 |
||
PRINT |
PRINT |
||
END SUB</ |
END SUB</syntaxhighlight> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Generate random numbers without repeating a value. Nigel Galloway: August 27th., 2021 |
// Generate random numbers without repeating a value. Nigel Galloway: August 27th., 2021 |
||
MathNet.Numerics.Combinatorics.GeneratePermutation 20|>Array.map((+)1)|>Array.iter(printf "%d "); printfn "" |
MathNet.Numerics.Combinatorics.GeneratePermutation 20|>Array.map((+)1)|>Array.iter(printf "%d "); printfn "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 294: | Line 294: | ||
Generating a random permutation of 1..20: |
Generating a random permutation of 1..20: |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: kernel math.combinatorics math.ranges prettyprint random |
||
sequences ; |
sequences ; |
||
Line 300: | Line 300: | ||
[ length dup nPk random ] keep permutation ; |
[ length dup nPk random ] keep permutation ; |
||
20 [1,b] random-permutation .</ |
20 [1,b] random-permutation .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 307: | Line 307: | ||
Shuffling 1..20: |
Shuffling 1..20: |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: math.ranges prettyprint random vectors ; |
||
20 [1,b] >vector randomize .</ |
20 [1,b] >vector randomize .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 316: | Line 316: | ||
Sampling 20 elements from 1..20: |
Sampling 20 elements from 1..20: |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: math.ranges prettyprint random ; |
||
20 [1,b] 20 sample .</ |
20 [1,b] 20 sample .</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 326: | Line 326: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">Sub pRand |
||
Dim As Integer randCheck(20), nr = 1 |
Dim As Integer randCheck(20), nr = 1 |
||
Do |
Do |
||
Line 347: | Line 347: | ||
pRand() |
pRand() |
||
Next num |
Next num |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 361: | Line 361: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
This uses Go's 'native' random number generator which internally uses a custom algorithm attributed to D P Mitchell and J A Reeds and can generate non-negative random integers in the 64-bit range. |
This uses Go's 'native' random number generator which internally uses a custom algorithm attributed to D P Mitchell and J A Reeds and can generate non-negative random integers in the 64-bit range. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 398: | Line 398: | ||
generate(1, 20) |
generate(1, 20) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 411: | Line 411: | ||
<br> |
<br> |
||
Alternatively and far more efficiently, we can simply create a list of the required numbers and randomly shuffle them. Go has a standard library function for this which uses the Fisher-Yates (aka Knuth) shuffle. |
Alternatively and far more efficiently, we can simply create a list of the required numbers and randomly shuffle them. Go has a standard library function for this which uses the Fisher-Yates (aka Knuth) shuffle. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 432: | Line 432: | ||
fmt.Println(s[1 : len(s)-2]) |
fmt.Println(s[1 : len(s)-2]) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 444: | Line 444: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (sortBy) |
||
import Data.Ord (comparing) |
import Data.Ord (comparing) |
||
import System.Random (newStdGen, randomRs) |
import System.Random (newStdGen, randomRs) |
||
Line 459: | Line 459: | ||
main = |
main = |
||
inRandomOrder [1 .. 20] |
inRandomOrder [1 .. 20] |
||
>>= print</ |
>>= print</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
For example: |
For example: |
||
Line 465: | Line 465: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">import java.util.*; |
||
public class RandomShuffle { |
public class RandomShuffle { |
||
Line 476: | Line 476: | ||
System.out.println(list); |
System.out.println(list); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 485: | Line 485: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
"use strict"; |
"use strict"; |
||
Line 540: | Line 540: | ||
// MAIN --- |
// MAIN --- |
||
return JSON.stringify(main()); |
return JSON.stringify(main()); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
For example: |
For example: |
||
Line 551: | Line 551: | ||
In this entry, an external source of entropy is used to define a jq |
In this entry, an external source of entropy is used to define a jq |
||
filter, `knuthShuffle`, so that the specific task can then be accomplished using the expression: |
filter, `knuthShuffle`, so that the specific task can then be accomplished using the expression: |
||
< |
<syntaxhighlight lang="jq">[range(1;21)] | knuthShuffle</syntaxhighlight> |
||
In the following, a bash or bash-like scripting environment is assumed, and the jq command is assumed to be "jq". |
In the following, a bash or bash-like scripting environment is assumed, and the jq command is assumed to be "jq". |
||
<syntaxhighlight lang="sh"> |
|||
<lang sh> |
|||
< /dev/urandom tr -cd '0-9' | fold -w 1 | jq -MRcnr -f program.jq |
< /dev/urandom tr -cd '0-9' | fold -w 1 | jq -MRcnr -f program.jq |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''program.jq''' |
'''program.jq''' |
||
< |
<syntaxhighlight lang="jq"># Output: a prn in range(0;$n) where $n is `.` |
||
def prn: |
def prn: |
||
if . == 1 then 0 |
if . == 1 then 0 |
||
Line 581: | Line 581: | ||
# The task: |
# The task: |
||
[range(1;21)] | knuthShuffle</ |
[range(1;21)] | knuthShuffle</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 608: | Line 608: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Julia's Random module contains a function called `randperm(n::Integer)` which constructs a random permutation of integers from 1 to n. |
Julia's Random module contains a function called `randperm(n::Integer)` which constructs a random permutation of integers from 1 to n. |
||
< |
<syntaxhighlight lang="julia">using Random |
||
@show randperm(20) |
@show randperm(20) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
randperm(20) = [20, 2, 5, 6, 18, 14, 12,4, 13, 7, 15, 3, 19, 17, 1, 9, 16, 11, 10] |
randperm(20) = [20, 2, 5, 6, 18, 14, 12,4, 13, 7, 15, 3, 19, 17, 1, 9, 16, 11, 10] |
||
</pre> |
</pre> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<lang |
<syntaxhighlight lang="mathematica">RandomSample[Range@20]</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
Line 627: | Line 627: | ||
Here, we have defined a procedure which accepts a slice <code>a..b</code> as argument and returns a shuffled sequence of values from a to b. It uses the same algorithm as in Wren solution, i.e. a list to keep track of generated values. |
Here, we have defined a procedure which accepts a slice <code>a..b</code> as argument and returns a shuffled sequence of values from a to b. It uses the same algorithm as in Wren solution, i.e. a list to keep track of generated values. |
||
< |
<syntaxhighlight lang="nim">import random |
||
randomize() |
randomize() |
||
Line 643: | Line 643: | ||
for i in 1..5: |
for i in 1..5: |
||
echo generate(1..20)</ |
echo generate(1..20)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 654: | Line 654: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Just shuffle... |
Just shuffle... |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Generate_random_numbers_without_repeating_a_value |
use strict; # https://rosettacode.org/wiki/Generate_random_numbers_without_repeating_a_value |
||
Line 660: | Line 660: | ||
use List::Util qw( shuffle ); |
use List::Util qw( shuffle ); |
||
print "@{[ shuffle 1 .. 20 ]}\n" for 1 .. 5;</ |
print "@{[ shuffle 1 .. 20 ]}\n" for 1 .. 5;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 673: | Line 673: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Trival use of standard builtins. Progressively filtering the output of rand(20) would gain nothing except wasted cycles. Normally I would use "with javascript_semantics", or equivalently just "with js", to explicitly specify/verify the code can be run on both the desktop ''and'' in a web browser, however here that somehow seems like overkill. |
Trival use of standard builtins. Progressively filtering the output of rand(20) would gain nothing except wasted cycles. Normally I would use "with javascript_semantics", or equivalently just "with js", to explicitly specify/verify the code can be run on both the desktop ''and'' in a web browser, however here that somehow seems like overkill. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">))</span> |
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 682: | Line 682: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python"> |
||
import random |
import random |
||
print(random.sample(range(1, 21), 20)) |
print(random.sample(range(1, 21), 20)) |
||
</ |
</syntaxhighlight>{{out}}[14, 15, 3, 18, 4, 11, 16, 10, 12, 20, 13, 1, 6, 7, 2, 17, 5, 9, 19, 8] |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
Line 711: | Line 711: | ||
=={{header|R}}== |
=={{header|R}}== |
||
R makes this so easy that it feels like you've missed the point. |
R makes this so easy that it feels like you've missed the point. |
||
<lang |
<syntaxhighlight lang="rsplus">sample(20)</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Raku has three distinct "random" functions built in. rand() for when you want some fraction between 0 and 1. roll() when you want to select elements from a collection with replacement (rolls of a die). And pick() for when you want to select some elements from a collection ''without'' replacement. (pick a card, any card, or two cards or 10 cards...). If you want to select ''all'' the elements in random order, just pick 'whatever'. Here we'll pick all from 1 to 20, 5 times using the repetition operator. |
Raku has three distinct "random" functions built in. rand() for when you want some fraction between 0 and 1. roll() when you want to select elements from a collection with replacement (rolls of a die). And pick() for when you want to select some elements from a collection ''without'' replacement. (pick a card, any card, or two cards or 10 cards...). If you want to select ''all'' the elements in random order, just pick 'whatever'. Here we'll pick all from 1 to 20, 5 times using the repetition operator. |
||
<lang |
<syntaxhighlight lang="raku" line>.put for (1..20).pick(*) xx 5</syntaxhighlight> |
||
{{out|Sample output}} |
{{out|Sample output}} |
||
<pre>20 4 5 7 15 19 2 16 8 6 3 12 14 13 10 18 9 17 1 11 |
<pre>20 4 5 7 15 19 2 16 8 6 3 12 14 13 10 18 9 17 1 11 |
||
Line 732: | Line 732: | ||
With the method/algorithm used herein, there are no random numbers being discarded (due to possible |
With the method/algorithm used herein, there are no random numbers being discarded (due to possible |
||
<br>duplicates) because there cannot ''be'' any duplicates. |
<br>duplicates) because there cannot ''be'' any duplicates. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program generates & displays a list of random integers (1 ──► N) with no repeats.*/ |
||
parse arg n cols seed . /*obtain optional argument from the CL.*/ |
parse arg n cols seed . /*obtain optional argument from the CL.*/ |
||
if n=='' | n=="," then n= 20 /*Not specified? Then use the default.*/ |
if n=='' | n=="," then n= 20 /*Not specified? Then use the default.*/ |
||
Line 759: | Line 759: | ||
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible show residual output.*/ |
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible show residual output.*/ |
||
say '───────┴'center("" , 1 + cols*(w+1), '─'); say |
say '───────┴'center("" , 1 + cols*(w+1), '─'); say |
||
exit 0 /*stick a fork in it, we're all done. */</ |
exit 0 /*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 769: | Line 769: | ||
</pre> |
</pre> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
see "working..." + nl |
see "working..." + nl |
||
decimals(3) |
decimals(3) |
||
Line 803: | Line 803: | ||
ok |
ok |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 817: | Line 817: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">nums = (1..20).to_a |
||
5.times{ puts nums.shuffle.join(" ") }</ |
5.times{ puts nums.shuffle.join(" ") }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 9 19 12 7 18 17 13 5 6 20 10 14 4 1 8 11 15 3 16 |
<pre>2 9 19 12 7 18 17 13 5 6 20 10 14 4 1 8 11 15 3 16 |
||
Line 827: | Line 827: | ||
</pre> |
</pre> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">// [dependencies] |
||
// rand = "0.7.2" |
// rand = "0.7.2" |
||
Line 837: | Line 837: | ||
v.shuffle(&mut rng); |
v.shuffle(&mut rng); |
||
println!("{:?}", v); |
println!("{:?}", v); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 846: | Line 846: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">var nums = (1..20).to_a |
||
5.times{ say nums.shuffle.join(" ") }</ |
5.times{ say nums.shuffle.join(" ") }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 859: | Line 859: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">var array = Array(1...20) |
||
array.shuffle() |
array.shuffle() |
||
print(array)</ |
print(array)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 870: | Line 870: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
< |
<syntaxhighlight lang="vlang">import rand |
||
import rand.seed |
import rand.seed |
||
Line 897: | Line 897: | ||
generate(1, 20) |
generate(1, 20) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Same as Go entry</pre> |
<pre>Same as Go entry</pre> |
||
Alternatively and far more efficiently, we can simply create a list of the required numbers and randomly shuffle them. Vlang has a standard library function for this which uses the Fisher-Yates (aka Knuth) shuffle. |
Alternatively and far more efficiently, we can simply create a list of the required numbers and randomly shuffle them. Vlang has a standard library function for this which uses the Fisher-Yates (aka Knuth) shuffle. |
||
< |
<syntaxhighlight lang="vlang">import rand |
||
import rand.seed |
import rand.seed |
||
Line 914: | Line 914: | ||
println(s[1 .. s.len-2]) |
println(s[1 .. s.len-2]) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 924: | Line 924: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
This uses Wren's 'native' pseudo-random number generator which internally uses WELL512a and can generate random integers in the 32-bit range. |
This uses Wren's 'native' pseudo-random number generator which internally uses WELL512a and can generate random integers in the 32-bit range. |
||
< |
<syntaxhighlight lang="ecmascript">import "random" for Random |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 944: | Line 944: | ||
// generate 5 sets say |
// generate 5 sets say |
||
for (i in 1..5) generate.call(1..20)</ |
for (i in 1..5) generate.call(1..20)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 957: | Line 957: | ||
<br> |
<br> |
||
Alternatively and far more efficiently, we can simply create a list of the required numbers and randomly shuffle them. Wren has a built-in function for this which uses the Fisher-Yates (aka Knuth) shuffle. |
Alternatively and far more efficiently, we can simply create a list of the required numbers and randomly shuffle them. Wren has a built-in function for this which uses the Fisher-Yates (aka Knuth) shuffle. |
||
< |
<syntaxhighlight lang="ecmascript">import "random" for Random |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 965: | Line 965: | ||
rand.shuffle(numbers) |
rand.shuffle(numbers) |
||
Fmt.print("$2d", numbers) |
Fmt.print("$2d", numbers) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 977: | Line 977: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">int Set, R; |
||
[Set:= 0; |
[Set:= 0; |
||
repeat R:= Ran(20); |
repeat R:= Ran(20); |
||
Line 984: | Line 984: | ||
IntOut(0, R+1); ChOut(0, ^ )]; |
IntOut(0, R+1); ChOut(0, ^ )]; |
||
until Set = $F_FFFF; |
until Set = $F_FFFF; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |