16
\$\begingroup\$

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.

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

37 Answers 37

1
2
1
\$\begingroup\$

Tcl, 90 bytes

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!

\$\endgroup\$
1
  • \$\begingroup\$ Try to outgolf got the same byte count: tio.run/… \$\endgroup\$
    – sergiol
    Mar 20, 2018 at 2:01
1
\$\begingroup\$

Thunno 2, 3 bytes

˵r

Try it online!

Explanation

˵r  # Implicit input
Ė    # Push [0..input]
 µr  # Random shuffle
     # Implicit output
\$\endgroup\$
1
\$\begingroup\$

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 }
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

RProgN 2, 3 bytes

0Rñ

Explained

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

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

Perl 5, 21 bytes

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

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.