0-n포함 부터 모든 숫자로 배열을 채워야 합니다. 숫자는 반복해서는 안됩니다. 그러나 무작위 순서 여야합니다.
규칙
모든 표준 코드 골프 규칙 및 표준 허점은 금지되어 있습니다.
배열은 의사 랜덤으로 생성되어야합니다. 가능한 모든 순열은 같은 확률을 가져야합니다.
입력
n 메타에 대한 I / O 포스트에 어떤 식 으로든 허용됩니다.
산출
0-n포괄적 인 숫자의 배열입니다 .
0-n포함 부터 모든 숫자로 배열을 채워야 합니다. 숫자는 반복해서는 안됩니다. 그러나 무작위 순서 여야합니다.
모든 표준 코드 골프 규칙 및 표준 허점은 금지되어 있습니다.
배열은 의사 랜덤으로 생성되어야합니다. 가능한 모든 순열은 같은 확률을 가져야합니다.
n 메타에 대한 I / O 포스트에 어떤 식 으로든 허용됩니다.
0-n포괄적 인 숫자의 배열입니다 .
답변:
{pick *,0..$_}
{ # bare block lambda with implicit parameter 「$_」
pick # choose randomly without repeats
*, # Whatever (all)
0 .. $_ # Range from 0, to the input (inclusive)
}
0rẊ
설명 :
0rẊ
0r Inclusive range 0 to input.
Ẋ Shuffle.
Implicit print.
‘ḶẊ
설명:
‘ḶẊ
‘ Input +1
Ḷ Range 0 to argument.
Ẋ Shuffle.
lambda n:sample(range(n+1),n+1)
from random import*
이 random.shuffle()있지만 그것을 반환하는 대신 장소에서 인수를 수정 ...
random.shuffle
lambda n:shuffle(range(n+1))출력을 어디에도 쓰지 않습니다.
QZ@q
Q % Implicitly input n. Add 1
Z@ % Random permutation of [1 2 ... n+1]
q % Subtract 1, element-wise. Implicitly display
ò öx
:Implicit input of integer U
ò :Generate array of 0 to U.
öx :Generate random permutation of array.
:Implicit output of result.
öx"포괄적 인"부분을 발견 할 때까지 충분 하다고 생각했습니다 . (당신은 xbtw를 거의 다른 것으로 바꿀 수 있습니다 )
Outgolfer Erik 덕분에 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;}
@ OlivierGrégoire 덕분에 -3 바이트 및 버그 수정 . @Jakob
덕분에 -4 바이트 .
를 제거하여 -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. 수정 및 골프 : for(n++;--n>=0;l.add(n));. 또한 배열을 반환 할 필요가 없다고 말합니다. 배열과 목록은 대부분의 언어에서 동일하므로 목록을 반환하십시오.
>이어야한다 >=.
Stack대신 a 를 사용하고 Vector루프를로 변경하십시오 for(;n>=0;l.add(n--));. 그리고 a를 돌려주는 java.util.List것은 확실히 좋습니다.
.S정수 인수와 동일 .SU하고, [0..n]같은 코딩 할 수 있습니다 Uh당신이 사용할 수 있도록, .SUh다음이되는, .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;}}
도중에 배열의 끝에서 초기화하고 나가기 전에 임의의 요소로 교체하는 재귀 함수.
n > 98?
a더 규칙에 맞게 파라로 변경 ?
A…·⁰NβFβ«AβδA‽δθPIθ↓A⟦⟧βFδ¿⁻θκ⊞βκ
온라인으로 사용해보십시오! 링크는 자세한 버전의 코드입니다.
분명히 Charcoal의 목록에서 요소를 제거하는 데 17 바이트가 걸립니다.
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