Generate random numbers without repeating a value: Difference between revisions

Added Easylang
m (syntax highlighting fixup automation)
(Added Easylang)
 
(15 intermediate revisions by 10 users not shown)
Line 99:
 
14 2 1 18 10 7 13 11 17 4 20 9 6 3 16 19 5 15 12 8
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
-- Generate the integers 1 .. 20 in random order
-- J. Carter 2023 Apr
 
with Ada.Numerics.Discrete_Random;
with Ada.Text_IO;
 
procedure Random_20 is
subtype Desired_Value is Integer range 1 .. 20;
package Desired_Random is new Ada.Numerics.Discrete_Random (Result_Subtype => Desired_Value);
type Desired_List is array (Desired_Value) of Desired_Value;
List : Desired_List;
Gen : Desired_Random.Generator;
Idx : Desired_Value;
begin -- Random_20
Fill : for I in List'Range loop
List (I) := I;
end loop Fill;
 
Desired_Random.Reset (Gen => Gen);
Randomize : for I in List'Range loop
Idx := Desired_Random.Random (Gen);
Swap : declare
Temp : Desired_Value := List (Idx);
begin -- Swap
List (Idx) := List (I);
List (I) := Temp;
end Swap;
end loop Randomize;
Print : for V of List loop
Ada.Text_IO.Put (Item => V'Image);
end loop Print;
Ada.Text_IO.New_Line;
end Random_20;
</syntaxhighlight>
{{out}}
<pre>
8 3 18 12 2 20 17 11 5 13 9 15 6 14 19 4 16 1 10 7
17 1 7 10 8 9 6 11 19 20 2 15 13 14 12 4 16 18 3 5
</pre>
 
Line 280 ⟶ 329:
PRINT
END SUB</syntaxhighlight>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Creates a "TRandomizer" object which makes it easy to setup and use multiple non repeating randomizers. Since the objects are independent, each one can have different parameters.
 
<syntaxhighlight lang="Delphi">
 
{Create randomize object to make it easier to use}
 
type TRandomizer = class(TObject)
private
FSize: integer;
procedure SetSize(const Value: integer);
protected
procedure Randomize;
public
Numbers: array of Integer;
constructor Create;
property Size: integer read FSize write SetSize;
end;
 
{ TRandomizer }
 
constructor TRandomizer.Create;
begin
Size:=20;
end;
 
procedure TRandomizer.Randomize;
var I,Inx1,Inx2,T: integer;
begin
for I:=1 to 100 do
begin
Inx1:=Random(Length(Numbers));
Inx2:=Random(Length(Numbers));
T:=Numbers[Inx1];
Numbers[Inx1]:=Numbers[Inx2];
Numbers[Inx2]:=T;
end;
 
end;
 
procedure TRandomizer.SetSize(const Value: integer);
var I: integer;
begin
if FSize<>Value then
begin
FSize:=Value;
SetLength(Numbers,FSize);
for I:=0 to FSize-1 do Numbers[I]:=I+1;
Randomize;
end;
end;
 
{------------------------------------------------------------------------------}
 
procedure ShowRandomNumbers(Memo: TMemo);
var RD: TRandomizer;
var S: string;
var I,J: integer;
begin
RD:=TRandomizer.Create;
try
RD.Size:=20;
for J:=1 to 5 do
begin
RD.Randomize;
S:='[';
for I:=0 to High(RD.Numbers) do
S:=S+Format('%3D',[RD.Numbers[I]]);
S:=S+']';
Memo.Lines.Add(S);
end;
finally RD.Free; end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
[ 20 12 9 19 2 8 16 14 10 4 11 6 18 5 7 1 17 3 15 13]
[ 7 12 20 13 4 19 8 3 17 5 2 6 9 1 15 11 18 16 10 14]
[ 19 12 7 20 6 16 1 10 13 15 4 2 8 9 3 17 11 18 14 5]
[ 18 13 12 20 8 2 3 10 4 16 19 1 7 5 11 15 9 6 17 14]
[ 17 20 19 3 6 10 12 9 5 16 18 7 8 13 4 15 1 11 14 2]
</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 463 ⟶ 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 482 ⟶ 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 607 ⟶ 795:
 
=={{header|Julia}}==
Julia's Random module contains a function called `randpermshuffle(n[rng=GLOBAL_RNG,] v::IntegerAbstractArray)` which constructs a randomrandomly permutationpermuted copy of integers from 1 to nv.
<syntaxhighlight lang="julia">using Random
Random.seed!(1234567)
@show randperm(20)
 
</syntaxhighlight>{{out}}
# 1. built in
 
@show shuffle(1:20)
 
# 2. from standard random generator rand()
 
myshuffle(urn::AbstractVector) =
map(length(urn):-1:1) do len
ball = urn[ceil(Int, rand() * len)]
urn = setdiff(urn, ball)
ball
end
 
@show myshuffle(1:20);</syntaxhighlight>{{out}}
<pre>
randpermshuffle(1:20) = [204, 217, 515, 614, 1811, 145, 12,418, 136, 7, 158, 316, 193, 1720, 113, 912, 1619, 112, 10, 1, 9]
myshuffle(1:20) = [5, 10, 19, 3, 15, 13, 18, 1, 6, 8, 2, 11, 4, 20, 17, 14, 9, 16, 7, 12]
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">RandomSample[Range@20]</syntaxhighlight>
Line 682 ⟶ 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>
[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 716 ⟶ 935:
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.
 
Pick random elements ''without'' replacement
<syntaxhighlight lang="raku" line>.put for (1..20).pick(*) xx 5</syntaxhighlight>
{{out|Sample output}}
Line 723 ⟶ 943:
7 5 15 11 12 18 17 3 20 6 13 19 14 2 16 10 4 9 8 1
19 12 4 7 3 20 13 17 5 8 6 15 10 18 1 11 2 14 16 9</pre>
 
Pick random elements ''with'' replacement
<syntaxhighlight lang="raku" line>.put for (1..20).roll(20) xx 5</syntaxhighlight>
{{out|Sample output}}
<pre>16 20 15 17 13 4 19 1 3 8 4 12 13 4 4 5 14 17 10 14
12 6 8 8 9 6 2 4 16 8 4 3 14 8 19 20 5 12 7 15
20 4 20 16 14 6 15 15 16 18 5 19 20 3 14 11 2 7 13 12
3 19 11 13 9 4 5 11 2 20 16 17 14 18 12 10 4 1 13 2
17 20 12 17 19 4 20 20 14 8 2 19 2 12 18 14 4 14 10 8</pre>
 
=={{header|REXX}}==
Line 814 ⟶ 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 826 ⟶ 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 868 ⟶ 1,123:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">import rand
import rand.seed
 
Line 924 ⟶ 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 957 ⟶ 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,983

edits