Generate random numbers without repeating a value: Difference between revisions

Added Easylang
m (Remove stray formatting text from code)
(Added Easylang)
 
(8 intermediate revisions by 5 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 600 ⟶ 619:
For example:
<pre>[16,1,3,9,8,20,12,18,11,19,2,14,5,6,13,15,17,10,7,4]</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">>: ?~ 20</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
</syntaxhighlight>
<syntaxhighlight lang="java">
int[] randomList() {
/* 'Set' allows only unique values */
/* 'LinkedHashSet' will preserve the input order */
Set<Integer> set = new LinkedHashSet<>();
Random random = new Random();
while (set.size() < 20)
set.add(random.nextInt(1, 21));
int[] values = new int[set.size()];
/* 'Set' does not have a 'get' method */
Iterator<Integer> iterator = set.iterator();
int index = 0;
while (iterator.hasNext())
values[index++] = iterator.next();
return values;
}
</syntaxhighlight>
<pre>
8, 3, 2, 17, 11, 4, 6, 15, 20, 9, 14, 10, 5, 19, 18, 7, 12, 13, 1, 16
</pre>
<pre>
6, 7, 20, 14, 1, 2, 10, 5, 13, 8, 4, 12, 16, 15, 17, 11, 18, 3, 19, 9
</pre>
<br />
<syntaxhighlight lang="java">import java.util.*;
 
Line 619 ⟶ 671:
[19, 15, 10, 6, 17, 13, 14, 9, 2, 20, 3, 18, 8, 16, 7, 12, 1, 4, 5, 11]
</pre>
 
 
=={{header|JavaScript}}==
Line 835 ⟶ 886:
 
=={{header|Python}}==
===Version 1===
<syntaxhighlight lang="python">
import random
 
print(random.sample(range(1, 21), 20))
</syntaxhighlight>
</syntaxhighlight>{{out}}[14, 15, 3, 18, 4, 11, 16, 10, 12, 20, 13, 1, 6, 7, 2, 17, 5, 9, 19, 8]
{{out}}
<pre>
</syntaxhighlight>{{out}}[14, 15, 3, 18, 4, 11, 16, 10, 12, 20, 13, 1, 6, 7, 2, 17, 5, 9, 19, 8]
</pre>
 
===Version 2===
<syntaxhighlight lang="python">
import random as r
 
def GenerateRandomSet(n: int) -> list:
set_ = list(range(1, n+1))
r.shuffle(set_)
return set_
</syntaxhighlight>
 
=={{header|Quackery}}==
Line 977 ⟶ 1,043:
Elapsed time = 0.008 s
done...
</pre>
 
=={{header|RPL}}==
'''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'''
n →LIST
≫ ≫ '<span style="color:blue">SHUFFLE</span>' STO
'''Using Knuth shuffle'''
 
<code>KNUTH</code> is defined at [[Knuth shuffle#RPL|Knuth shuffle]]
≪ { }
1 ROT '''FOR''' j j + '''NEXT'''
<span style="color:blue">KNUTH </span>
≫ '<span style="color:blue">SHUFFLE</span>' STO
 
20 <span style="color:blue">SHUFFLE</span>
{{out}}
<pre>
1: { 3 13 14 1 7 10 4 9 12 11 16 15 18 17 20 6 19 8 2 5 }
</pre>
 
Line 989 ⟶ 1,080:
2 16 13 12 6 18 14 4 15 7 9 10 8 11 19 5 17 1 3 20
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">// [dependencies]
Line 1,087 ⟶ 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,120 ⟶ 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