Generate random numbers without repeating a value: Difference between revisions

Added Easylang
(→‎J: add)
(Added Easylang)
 
(4 intermediate revisions by 2 users not shown)
Line 417:
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc shuffle . a[] .
for i = len a[] downto 2
r = randint i
swap a[r] a[i]
.
.
for i to 20
arr[] &= i
.
shuffle arr[]
print arr[]
</syntaxhighlight>
{{out}}
<pre>
[ 18 12 19 10 11 4 2 3 6 20 8 16 15 9 7 17 1 14 5 13 ]
</pre>
 
=={{header|F_Sharp|F#}}==
Line 1,029 ⟶ 1,048:
'''Stand-alone implementation'''
≪ 0 → n r
≪ 1 n '''FOR''' j j '''NEXT''' <span style="color:grey">@ fill stack with 1, 2,..n</span>
1 n '''START'''
n 1 - RAND * CEIL 1 + 'r' STO
r ROLL SWAP r ROLLD <span style="color:grey">@ swap 2 stack levels randomly, n times</span>
n ROLL
'''NEXT'''
Line 1,160 ⟶ 1,179:
{{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.
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var rand = Random.new()
Line 1,193 ⟶ 1,212:
<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.
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var rand = Random.new()
1,995

edits