您必须使用每个数字(0-n
含)来填充数组。没有数字应该重复。但是,它们必须是随机顺序的。
规则
禁止所有标准代码高尔夫球规则和标准漏洞
该数组必须是伪随机生成的。每个可能的排列都应具有相等的概率。
输入值
n
以meta的I / O帖子中允许的任何方式。
输出量
数字数组从0-n
包容性开始逐渐增加。
您必须使用每个数字(0-n
含)来填充数组。没有数字应该重复。但是,它们必须是随机顺序的。
禁止所有标准代码高尔夫球规则和标准漏洞
该数组必须是伪随机生成的。每个可能的排列都应具有相等的概率。
n
以meta的I / O帖子中允许的任何方式。
数字数组从0-n
包容性开始逐渐增加。
Answers:
{pick *,0..$_}
{ # bare block lambda with implicit parameter 「$_」
pick # choose randomly without repeats
*, # Whatever (all)
0 .. $_ # Range from 0, to the input (inclusive)
}
lambda n:sample(range(n+1),n+1)
from random import*
有,random.shuffle()
但是它修改了参数而不是返回参数。
random.shuffle
lambda n:shuffle(range(n+1))
不会在任何地方写入输出。
多亏了Egg the Outgolfer删除了1个字节。
{),mr}
这是一个匿名块(函数),它从堆栈中获取一个整数并将其替换为结果。 在线尝试!
{ e# Begin block
) e# Increment: n+1
, e# Range: [0 1 ... n]
mr e# Shuffle
} e# End block
{),mr}
1个字节的短?
import java.util.*;n->{List l=new Stack();for(;n>=0;l.add(n--));Collections.shuffle(l);return l;}
-3个字节,并通过@OlivierGrégoire修复了错误。
-4个字节,感谢@Jakob。
删除-10个字节.toArray()
。
说明:
import java.util.*; // Required import for List, Stack and Collections
n->{ // Method with integer parameter and Object-array return-type
List l=new Stack(); // Initialize a List
for(;n>=0;l.add(n--)); // Loop to fill the list with 0 through `n`
Collections.shuffle(l); // Randomly shuffle the List
return l; // Convert the List to an Object-array and return it
} // End of method
n
. Fix and golf: for(n++;--n>=0;l.add(n));
. Also, I say you don't need to return an array. Array and list are the same in most language, so just return the list.
>
should be >=
.
Stack
instead of a Vector
and change your loop to for(;n>=0;l.add(n--));
. And returning a java.util.List
is definitely fine.
.S
with an integer argument is the same as .SU
, and [0..n]
can be coded as Uh
, so you can use .SUh
, which then becomes .Sh
.
a[99],z,y;f(n){if(n){a[n]=--n;f(n);z=a[n];a[n]=a[y=rand()%(n+1)];a[y]=z;}}
Recursive function that initializes from the array's end on the way in, and swaps with a random element before it on the way out.
n > 98
?
a
into a para to fit the rule more?
A…·⁰NβFβ«AβδA‽δθPIθ↓A⟦⟧βFδ¿⁻θκ⊞βκ
Try it online! Link is to verbose version of code.
Apparently it takes 17 bytes to remove an element from a list in Charcoal.
Edit: These days it only takes three bytes, assuming you want to remove all occurrences of the item from the list. This plus other Charcoal changes cut the answer down to 21 bytes: Try it online!
?⍨1+⊢
Assumes ⎕IO←0
, which is default on many machines.
⊢
the right argument
1+
add 1 to it
?⍨
generate numbers 0 .. 1+⊢
-1 and randomly deal them in an array so that no two numbers repeat
Solution:
{(0-x)?1+x}
Example:
q){(0-x)?1+x}10
5 9 7 1 2 4 8 0 3 10
q){(0-x)?1+x}10
6 10 2 8 4 5 9 0 7 3
q){(0-x)?1+x}10
9 6 4 1 10 8 2 7 0 5
Explanation:
Use the ?
operator with a negative input to give the full list of 0->n
without duplicates:
{(0-x)?1+x} / solution
{ } / lambda expression
x / implicit input
1+ / add one
? / rand
(0-x) / negate x, 'dont put item back in the bag'
randIntNoRep(0,Ans
Yep, a builtin. randIntNoRep(
is a two-byte token, and Ans
is one byte.
Ans→N
seq(X,X,0,N→L₁
rand(N+1→L₂
SortA(L₂,L₁
L₁
Straight from tibasicdev. Probably golfable, but I haven't found anything yet.
What this does: Sorts a random array, moving elements of the second arg (L₁
here) in the same way as their corresponding elements.
n=>[...Array(n+1).keys()].sort(_=>.5-Math.random())
f(5)
10 times and 5
has been one of the last two items every time.
1,5,4,0,2,3
& 1,0,2,5,3,4
. EDIT: And a few more prnt.sc/fe0goe
f(5)
1e5 times and finds the average position of each number in the results. The resulting array was [ 1.42791, 1.43701, 2.00557, 2.6979, 3.3993, 4.03231 ]
, so I don't think it's uniform. (code)
n=>(a=[...Array(n).keys(),n++]).reduce((a,v,i)=>([a[i],a[j]]=[a[j=n*Math.random()|0],v],a),a)
?
random()
isn't uniform. See (for example) en.wikipedia.org/wiki/BrowserChoice.eu#Criticism
@lXp
Y!`n
zi&
0r
Push zero on the stack, read an integer, construct a range and shuffle it:
Y
zi
0r
Set a catch mark, test length for 0, and (in that case) exit:
@lX
!`
Else print the value, a newline, and jump back to the length test:
p
n
&
(I had to change the code because I realized I misread the question and had constructed a range from 1-n, not 0-n.)
Mostly losing to the need to seed the PRNG.
import(."fmt";."math/rand";."time")
func f(n int){Seed(Now().UnixNano());Println(Perm(n+1))}
->n{[*0..n].shuffle}
Code
>r [] ' a:push 0 r> loop a:shuffle
SED (Stack Effect Diagram) is n -- a
Usage and example
ok> 5 >r [] ' a:push 0 r> loop a:shuffle .
[2,5,0,3,1,4]
(?@!A.i.)>:
>: | Increment
(?@!A.i.) | Fork, (f g h) n is evaluated as (f n) g (h n)
i. | Integers in range [0,n) inclusive
?@! | Random integer in the range [0, n!)
A. | Permute right argument according to left
Examples:
0 A. i.>:5
0 1 2 3 4 5
1 A. i.>:5
0 1 2 3 5 4
(?@!A.i.)>: 5
2 3 5 1 0 4
(?@!A.i.)>: 5
0 3 5 1 2 4