# Randomize the scalars of an array

You must fill an array with every number from 0-n inclusive. No numbers should repeat. However they must be in a random order.

# Rules

All standard rules and standard loopholes are banned

The array must be generated pseudo-randomly. Every possible permutation should have a equal probability.

# Input

n in any way allowed in the I/O post on meta.

# Output

The array of numbers scrambled from 0-n inclusive.

• the output can be separated by newlines? Commented May 30, 2017 at 20:48
• @Riley opps that was meant to be gone sorry.
– user63187
Commented May 31, 2017 at 0:48
• @DrnglVrgs yes it can
– user63187
Commented May 31, 2017 at 0:48
• By "numbers" I assume you mean "integers"? Commented Aug 10, 2017 at 1:12
• @KevinCruijssen IMO lists = array but with searching support. So sure use a list
– user63187
Commented Aug 11, 2017 at 14:59

proc R n {lsort -c {try expr\ rand()>.5 on 9} [if [incr n -1]+2 {concat [R $n] [incr n]}]}  Try it online! # Tcl, 96 bytes proc R n {proc f a\ b {expr rand()>.5} set i -1 while \$i<$n {lappend L [incr i]} lsort -c f$L}


Try it online!

• Try to outgolf got the same byte count: tio.run/… Commented Mar 20, 2018 at 2:01

# Thunno 2, 3 bytes

Ėµr


Try it online!

#### Explanation

Ėµr  # Implicit input
Ė    # Push [0..input]
µr  # Random shuffle
# Implicit output


# Factor, 25 bytes

[ dup [0,b] swap sample ]


Try it online!

        ! 10
dup     ! 10 10
[0,b]   ! 10 { 0 1 2 3 4 5 6 7 8 9 10 }
swap    ! { 0 1 2 3 4 5 6 7 8 9 10 } 10
sample  ! { 3 6 9 10 4 8 2 5 7 0 }


# Arturo, 17 bytes

$=>[shuffle@0..&]  Try it! $=>[          ; a function where input is assigned to &
0..&      ; a range from 0 to input inclusive
@         ; reify the range (make it a concrete array)
shuffle   ; shuffle it
]             ; end function


# RProgN 2, 3 bytes

0Rñ


## Explained

0Rñ
0R  # Range of numbers 0-n (Implicit input)
ñ # Shuffle, Implicit output.


Try it online!

# AWK, 82 80 bytes

{for(n=$1+2;++j<n;$j=j-1);for(srand();--n;){t=$(i=int(n*rand()+1));$i=$n;$n=t}}1


Try it online!

AWK doesn't have a built-in shuffle, so this is the best I could come up with.
The srand() call is needed to prevent each execution from returning the same randomized sequence.

Thanks go to @ceilingcat for moving the call to srand() and the arithmetic tweaks to save 2 bytes.

• @ceilingcat if we keep managing to shave off 2 bytes every 5 years, then in 200 years we'll have a 0-byte solution :-) Commented Oct 25, 2023 at 17:48

# Perl 5, 21 bytes

@a{0..<>}=$,=$/;say%a


Try it online!