| Bytes | Lang | Time | Link |
|---|---|---|---|
| 014 | APLDyalog Unicode | 250509T181346Z | Mat_rdv |
| 065 | TIBASIC TI83 Plus | 250424T162828Z | madeforl |
| 140 | C# 10 | 250423T131633Z | juanferr |
| 032 | Zsh + GNU coreutils | 250427T172925Z | corvus_1 |
| 063 | Pharo | 250426T010154Z | chunes |
| 046 | Arturo | 250425T224146Z | chunes |
| 036 | R | 250425T060453Z | pajonk |
| 082 | Google Sheets | 250425T162935Z | doubleun |
| nan | Google Sheets | 250422T205133Z | z.. |
| 076 | CASIO BASIC CASIO fx9750GIII | 250424T153241Z | madeforl |
| 099 | C gcc | 250424T220733Z | AShelly |
| 112 | Maple | 250424T171045Z | dharr |
| 090 | Julia | 250423T185859Z | Sort of |
| 079 | JavaScript ES7 | 250422T211837Z | Arnauld |
| 051 | PowerShell | 250423T220301Z | Zaelin G |
| 011 | Uiua 0.16.0dev.1 | 250423T152416Z | Tbw |
| 054 | Perl 5 MListUtil=min MListMoreUtils=samples ln | 250423T144418Z | Xcali |
| 020 | APL+WIN | 250423T124913Z | Graham |
| 008 | 05AB1E | 250423T095710Z | Kevin Cr |
| 027 | Charcoal | 250423T092650Z | Neil |
| 077 | Python 3 | 250422T152742Z | Jitse |
| 043 | Wolfram Language Mathematica | 250423T040418Z | att |
| 007 | Vyxal | 250423T013222Z | lyxal |
| 042 | Ruby | 250423T004234Z | Value In |
| 005 | Jelly | 250422T193822Z | Jonathan |
APL(Dyalog Unicode), 14 bytes SBCS
∊⊢?⍨¨|⍨,⍨⌊⍤÷⍴⊢
X is the left argument, Y is the right.
Explanation
⊢ right argument, Y
⍴ reshape
÷ divide
⍤ postprocess
⌊ floor
⌊⍤÷ floor division; X//Y
⌊⍤÷⍴⊢ repeat Y (X//Y) times
⍨ swap left and right arguments
, concatenate
,⍨ append left argument to the end
⍨
| modulo
|⍨ Y%X (arguments are swapped)
|⍨,⍨⌊⍤÷⍴⊢ repeat Y while the sum is less than X, append the remainder
¨ each
⍨
? deal: A?B returns A different numbers in range 1..B
⊢
⊢?⍨¨ for each number N on the right: deal N times in range Y
⊢?⍨¨|⍨,⍨⌊⍤÷⍴⊢ solution but as vector of vectors: new subvector starts after exhausting all values in 1..Y
∊ enlist: make one vector from all values in order
∊⊢?⍨¨|⍨,⍨⌊⍤÷⍴⊢ solution
TI-BASIC (TI-83 Plus), 72 67 65 bytes
-5 bytes thanks to MarcMush
Input A
Input B
B→dim(L₁
For(N,1,A
If not(max(L₁
cumSum(1 or L₁→L₁
Repeat L₁(E
randInt(1,B→E
End
Disp L₁(E
0→L₁(E
End
TI calculators with OS 2.53MP (and the TI-84+/SE) have a RandIntNoRep function that would make this a lot easier, but I don't have one of those anymore. gotta look into upgrading
C# 10, 202 154 140 bytes
f=(x,y)=>{Random r=new();List<int> a=new();for(int i=-1;i<x/y;i++)a.AddRange(Enumerable.Range(1,y).OrderBy(x=>r.Next(y)));return a.Take(x);}
Accepts 2 ints and returns an IEnumerable<int>.
Try it on JDoodle.
Ungolfed:
Random r = new();
List<int> a = new();
for (int i = -1; i < x / y; i++) // Repeat 1 + x/y times:
a.AddRange(Enumerable.Range(1, y).OrderBy(x => r.Next(y))); // Get the range 1-y, sort it randomly and append to list
Console.WriteLine(string.Join(',', a.Take(x))); // Take the first x numbers from list and print them
C# 12, 198 150 138 bytes
Shorter empty list initialiser ([] vs new()).
f=(x,y)=>{Random r=new();List<int> a=[];for(int i=-1;i<x/y;i++)a.AddRange(Enumerable.Range(1,y).OrderBy(x=>r.Next(y)));return a.Take(x);}
Edit: Turned it into a function thanks to Themoonisacheese's comment.
Pharo, 63 bytes
[:x :y|((1to:x)flatCollect:[:x|(1to:y)asArray shuffle])first:x]
Google Sheets, 82 bytes
=query(tocol(map(1:1,lambda(_,sort(sequence(A3),randarray(A3),))),,1),"limit "&A2)
Put \$X\$ in cell A2, \$Y\$ in cell A3 and the formula in cell B2.

(cheats a bit)
Google Sheets, 103 91 bytes
-12 bytes thanks to Jonathan Allan
Expects \$X\$ in A1 and \$Y\$ in A2.
=QUERY(REDUCE(,SEQUENCE(A1),LAMBDA(a,_,{a;SORT(SEQUENCE(A2),RANDARRAY(A2),)})),"limit "&A1)
Stacks on top of each other \$X\$ randomized sequences of numbers from \$1..Y\$ and constrains the result to the first \$X\$ values.
CASIO BASIC (CASIO fx-9750GIII), 92 76 bytes
?→A
?→B
{0→List1
For 1→N To A
Not Max(List1⟹Seq(K,K,1,B,1→List1
Do
RanInt#(1,B
LpWhile Not List1[Ans
List1[Ans◢
0→List1[Ans
Next
I think this is right
input x first, then y
C (gcc), 99 bytes
f(x,y){int i,a[y],n=0;for(;x--;a[i]=a[--n]){
if(!n)for(;n<y;a[n++]=n);printf("%d ",a[i=rand()%n]);}}
A simple algorithm that (re)generates the pool when empty, and swaps each picked value with the shrinking end.
Maple, 112 bytes
proc(x,y)L:=[$y];S:=();to x do r:=rand(1..nops(L))();S,=L[r];L:=subsop(r=(),L);if L=[] then L:=[$y] fi od;S end;
proc(x,y)
L:=[$y]; # initialize list to [1..y]
S:=(); # initialize output sequence to NULL
to x do
r:=rand(1..nops(L))(); # find random index into current list
S,=L[r]; # augment sequence with new value
L:=subsop(r=(),L); # remove value from list
if L=[] then L:=[$y] fi # reset list if empty
od;
S; # return sequence
end;
I thought a recursive function would be shorter, but it wasn't
Julia, 90 bytes
+(X,Y)=begin Z=[];map(x->begin(Z==[])&&(Z=[1:Y...]);popat!(Z,rand(1:length(Z)))end,1:X)end
Grateful that rand() is in Base.
ATO doesn't have StatsBase but the following is 85 bytes:
using StatsBase
+(x,y)=[sample([1:y...],min(x,y);replace=false);(x>y) ? (x-y+y) : []]
JavaScript (ES7), 79 bytes
Expects (y)(x).
y=>g=(x,n,i=Math.random()*y)=>x?n>>i&1?[-~i,...g(x-1,n^1<<i)]:g(x,n||2**y-1):[]
Commented
y => // outer function taking y
g = ( // inner recursive function taking:
x, // x
n, // n = bit-mask, initially undefined
i = Math.random() // i = random value in [0, y[
* y // (implicitly floored in bitwise operations)
) => //
x ? // if x is not zero:
n >> i & 1 ? // if the i-th bit of n is set:
[ // update the output array:
-~i, // append floor(i) + 1
...g( // do a recursive call:
x - 1, // decrement x
n ^ 1 << i // clear the i-th bit of n
) // end of recursive call
] // end of array update
: // else:
g( // do a recursive call:
x, // pass x unchanged
n || // pass either n unchanged,
2 ** y - 1 // or with y bits set if cleared
) // end of recursive call
: // else:
[] // stop
Uiua 0.16.0-dev.1, 11 bytes SBCS
˜↙♭≡°⍆+1↯⤙⇡
Takes Y X on stack.
Explanation
˜↙♭≡°⍆+1↯⤙⇡
+1↯⤙⇡ # X copies of range 1...Y
≡°⍆ # randomize each (literally "un"-"sort")
˜↙♭ # flatten and take the first X
Perl 5 -MList::Util=min -MList::MoreUtils=samples -ln, 54 bytes
map{say}samples min($_,$t//=<>),1..$_;($t-=$_)>0&&redo
APL+WIN, 20 bytes.
Prompts for y followed by x.
x↑∊y?¨(⌈(x←⎕)÷y)⍴y←⎕
05AB1E, 8 bytes
иL€.r˜¹£
Inputs in the order \$X,Y\$. Outputs as a list.
A program derived from my answer to the related challenge would be 1 byte longer:
FIL.r)˜ć,
Inputs in the order \$X,Y\$. Outputs each value on a separated newline.
Explanation:
и # Repeat the second (implicit) input `Y` the first (implicit) input `X`
# amount of times as list
L # Convert each inner `Y` to a list in the range [1,Y]
€ # Map over each of those inner [1,Y]-lists
.r # Randomly shuffle it
˜ # Flatten this list of lists
¹£ # Only keep the first input `Y` amount of leading values
# (after which this list is output implicitly)
F # Loop the first (implicit) input `X` amount of times:
IL # Push a list in the range [1, second input Y]
.r # Randomly shuffle it
) # Wrap everything on the stack into a list
˜ # Flatten it
ć # Extract its head; push remainder-list and first item separately
, # Pop and print this first item with trailing newline
Charcoal, 27 bytes
NθFN«F∧¬υθ⊞υ⊕κ≔⟦‽υ⟧ι≔⁻υιυIι
Try it online! Link is to verbose version of code. Takes inputs in the order Y, X. Explanation:
Nθ
Input Y.
FN«
Repeat X times.
F∧¬υθ⊞υ⊕κ
If all of the values are exhausted, replace all the values 1..Y.
≔⟦‽υ⟧ι
Pick a random value and wrap it in a list.
≔⁻υιυ
Perform list subtraction to remove it from the values.
Iι
Output it. (As a list, this outputs each value on its own line.)
Python 3, 77 bytes
f=lambda x,y:x*[x]and sample(range(1,y+1),y)[:x]+f(x-y,y)
from random import*
-3 bytes thanks to Jonathan Allan
Vyxal, 7 bytes
ẋvÞ℅f⁰Ẏ
Taking a L here because shuffle is two bytes and because input cycling works against me
Ruby, 42 bytes
Rather conveniently, calling sample with a number bigger than the length of the array shuffles it (as if you called it with the array length), saving bytes over having to get the minimum of x and y.
f=->x,y{x>0?[*1..y].sample(x)+f[x-y,y]:[]}
Jelly, 5 bytes
Ẋ{ⱮFḣ
A dyadic Link that accepts the range size, \$Y\$, on the left and the desired count, \$X\$, on the right and yields a list of the \$X\$ draws from \$[1,Y]\$.
How?
Shuffle each of \$X\$ copies of \$[1,Y]\$, flatten this list of lists, and then only keep the first \$X\$.
Ẋ{ⱮFḣ - Link: non-negative integer, Y; non-negative integer, X
Ɱ - map across {v in [1..X]} with - f(Y, v):
{ - using Y as the arguments - f(Y, Y):
Ẋ - shuffle {[1..Y]}
F - flatten
ḣ - head to index {X}