14
\$\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\$
  • \$\begingroup\$ the output can be separated by newlines? \$\endgroup\$ – DrnglVrgs May 30 '17 at 20:48
  • \$\begingroup\$ @Riley opps that was meant to be gone sorry. \$\endgroup\$ – Christopher May 31 '17 at 0:48
  • \$\begingroup\$ @DrnglVrgs yes it can \$\endgroup\$ – Christopher May 31 '17 at 0:48
  • \$\begingroup\$ By "numbers" I assume you mean "integers"? \$\endgroup\$ – Zacharý Aug 10 '17 at 1:12
  • 1
    \$\begingroup\$ @KevinCruijssen IMO lists = array but with searching support. So sure use a list \$\endgroup\$ – Christopher Aug 11 '17 at 14:59

32 Answers 32

0
\$\begingroup\$

JavaScript ES6, 60 bytes, uniform distribute

f=n=>n?(z=f(n-1),z[n]=z[y=Math.random()*-~n|0],z[y]=n,z):[0]

s={};
for(i=0; i<1000; i++) k=f(2), s[k]=-~s[k];
for(i in s) console.log(i, s[i]);

\$\endgroup\$
0
\$\begingroup\$

AWK, 82 bytes

{srand();for(n=$1+1;j++<n;$j=j-1);for(;n;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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.