Diese Herausforderung ist recht einfach:
Sie erhalten ein Array positiver (ohne 0) Ganzzahlen und müssen ein zufälliges Element aus diesem Array auswählen.
Aber hier ist die Wendung:
Die Wahrscheinlichkeit, ein Element auszuwählen, hängt vom Wert der Ganzzahl ab, dh je größer die Ganzzahl wird, desto größer ist auch die Wahrscheinlichkeit, dass es ausgewählt wird!
Beispiel
Sie erhalten das Array [4, 1, 5]
.
In diesem Fall ist die Wahrscheinlichkeit der Auswahl von 4 gleich 4 geteilt durch die Summe aller Elemente im Array4 / ( 4 + 1 + 5 ) = 4 / 10 =
40%
.
Die Wahrscheinlichkeit für die Auswahl von 1 ist 1 / 10
oder 10%
.
Eingang
Eine Reihe von positiven ganzen Zahlen.
Ausgabe
Gibt die ausgewählte Ganzzahl zurück, wenn eine Methode verwendet wird, oder druckt sie direkt aus stdout
.
Regeln
- Dies ist Code-Golf, so dass der kürzeste Code in Bytes in jeder Sprache gewinnt.
- Standardlücken sind verboten.
quelle
R , 25 Bytes
Probieren Sie es online!
Erläuterung:
Nimmt eine Probe von
s
einer Größe1
ohne Ersatz mit Gewichtens
; Diese werden neu skaliert, um Wahrscheinlichkeiten zu sein.Verwenden Sie diesen Link, um die Verteilung zu überprüfen .
quelle
Pyth , 4 Bytes
Probieren Sie es hier aus.
Dank @Jakube wurde ein Byte mit einem eher ungewöhnlichen Ansatz gespeichert.
Pyth , 5 Bytes
Probieren Sie es hier aus!
Wie?
# 1
# 2
quelle
OsmL
oderOsmR
d
, dann Kartend
über einen Bereich ... Genie!CJam (9 Bytes)
Online-Demo . Dies ist ein vollständiges Programm, das Eingaben im CJam-Array-Format auf stdin übernimmt und das ausgewählte Element auf stdout ausgibt.
Präparation
quelle
Perl 6 , 20 Bytes
1 Byte dank @Brad Gilbert b2gills gespeichert.
Probieren Sie es online!
Dies benötigt 1 Listenargument. Wir haben 2 Kopien dieser Liste mit dem
xx
Operator gepackt. So erhalten@_ Zxx@_
wir eine Liste, in der das Element malx
dargestelltx
wird. Es wird dann zuBag
einer Sammlung gezwungen , in der Objekte zusammen mit der Häufigkeit ihres Auftretens in der Sammlung gespeichert werden. Schließlich wählen wir ein zufälliges Element aus dieser Sammlung mit auspick
, das die Zählungen berücksichtigt und The Right Thing ™ ausführt.quelle
{bag(@_ Z=>@_).pick}
{bag(@_ Zxx@_).pick}
Python 3.6 , 44 Bytes
Yay für Einbauten. Das andere
A
inchoices(A, A)
ist ein optionalesweights
Argument.Probieren Sie es online!
quelle
MATL ,
86 BytesProbieren Sie es bei MATL Online!
Erläuterung
quelle
Mathematica, 19 Bytes
quelle
Ruby, 32 bytes
Try it online!
quelle
Python 3, 62 bytes
Try it online!
quelle
Java (OpenJDK 8),
88878683 bytesTry it online!
quelle
for(r*=Math.random();;)
is needed, or if all you need isr*=Math.random()
.for(;;)
loop this would require a second (never reached) return statement after thefor(int i:a)...
to satisfy the compiler - which would be 3 bytes longer.for(int i:a)
is like aforeach
in C#. I had the same problem, but just used afor
that continually loops. Your new answer intrigues me, I might try and pilfer some of your ideas.J,
878 bytesThe 7 byter is invalid; I'll revert this to a previous edit when I get back to my computer in a day or two.
Try it online!
:( selecting random elements from an array is costly.
8 bytes
9 bytes
Explanation
quelle
?@+/
is(?@+)/
; I'm afraid you'll have to bump that up to 8 again…JavaScript (ES6), 50 bytes
Hopefully it's apparent how this works, but I'll explain it here anyway. It sorts the integers in descending order, then chooses one at random with a beta distrubution (1/2,1).
quelle
a=[4,1,5]
, you'll get about 18%1
, 24%4
, and 58%5
, which suggests you'll get that distribution with any input of length 3.05AB1E, 9 bytes
Try it online!
quelle
PowerShell, 27 bytes
Try it online!
Takes input
$args[0]
as a literal array. Loops through each element|%{...}
and each iteration constructs a new array,$_
of$_
elements -- e.g., for4
this will create an array@(4,4,4,4)
. Those array elements are then piped intoGet-Random
which will pluck out one of the elements with (pseudo) equal probability. Since, e.g., for@(4,1,5)
this gives us@(4,4,4,4,1,5,5,5,5,5)
this satisfies the probability requirements.quelle
C# (.NET Core),
93898776+18 = 94 bytesTry it online!
An extra 18 bytes for
using System.Linq;
Acknowledgements
11 bytes saved thanks to Nevay, whose random number implementation was a lot more concise (as well as being an
int
instead of adouble
).Degolfed
Explanation
Get a random number,
r
, between 0 and sum of elements. Then at each iteration subtract the current element fromr
. Ifr
is less than0
, then return this element. The idea is that there are bigger portions of the random number for the larger numbers in the array.quelle
a=>{int i=-1,r=new Random().Next(a.Sum());for(;r>=0;)r-=a[++i];return a[i];}
Japt, 7 bytes
Test it here
Explanation
Implicit input of array
U
.Map over the array passing each element through a function where
D
is the current element.Generate an array of length
D
and fill it withD
.Flatten.
Get a random element.
quelle
CJam, 5 bytes
Try it online! Note: seperate numbers by a space
quelle
Perl, 31 bytes
This assumes the input to be command line arguments. Note that it may run out of memory if the numbers are large.
quelle
Perl 5, 31 + 1 (-a) = 32 bytes
Try it online!
quelle
GolfScript, 17 bytes
Try it online!
quelle
Charcoal, 12 bytes
Try it online! Link is to verbose version of code. Since Charcoal tries to be too clever, I'm having to use semicolon-delimited input for the array. Explanation:
quelle
Haskell, 87 bytes
Try it online!
quelle
Javascript (ES6),
6154 bytes-7 bytes thanks to @Justin Mariner
Example code snippet
quelle
eval(a.join`+`)
instead ofreduce
.[].find(m=>(n-=m)<0,n=Math.random()*eval(a.join
+))
and call withinput::[].find(...)
Haskell,
7877 bytesTry it online! Usage example:
f [1,99]
probably yields99
.Explanation:
f
takes a list of integersl
and returns the randomly selected integer asIO Int
.l>>= \n->n<$[1..n]
constructs a list with each elementn
repeatedn
times.randomRIO(0,sum l-1)
yields an integer in the range from 0 to the length of the list of repeated elements, which is exactly the sum of all elements, minus one to avoid a out of bound exception.Bonus: 85 byte point-free version
Try it online!
quelle
Bash, 51 bytes
Takes space-separated or newline-separated input in one argument or multiple arguments.
Try it online!
Validate the random frequencies with a more complicated test case.
quelle
Java 8,
127122121 bytes-1 byte thanks to @Nevay.
Uses a similar approach as @ErikTheOutgolfer's Jelly answer, by adding
n
times the itemn
to the list, and then select one randomly from that list.Explanation:
Try it here.
quelle
#shuffle
call into the for loop to save 1 bytefor(int j=i;j-->0;Collections.shuffle(l))l.add(i);
.Dyalog APL, 8 bytes
Try it online!
How?
/⍨
,n
copies ofn
for eachn
in the argument.⌷⍨
, at the index of1?
, a random value between1
and+/
, the sum of the argumentquelle
GNU APL 1.2,
2623 bytes; 1.72119 bytesApproach inspired by Erik the Outgolfer's Jelly answer. Relies on
⎕IO
being 0 instead of 1, which is the default for GNU APL (sort of +5 bytes for⎕IO←0
).-3, -2 bytes thanks to @Zacharý
∇
function formAnonymous lambda form
For the explanation, I will use
⍵
to represent the argument passed to the function, but it is equivalent toR
in the∇
form.⍵∘.⍴⍵
computes the outer product on the list using the reshape (⍴
) operator. Effectively, this creates a table (like a multiplication table) but instead of multiplying, it repeats the element in the column a number of times equal to the element in the row. For the example given in the question, this is:0 0⍉⍵∘.⍴⍵
transposes the matrix and returns just the main diagonal. This gives us just the parts where the row and column in⍵∘.⍴⍵
were the same i.e. we repeated the number a number of times equal to its value. For the example, this is:∊
turns its argument into a list. Using the transpose (⍉
) operator, we got a vector containing 3 vectors. Enlist (∊
) turns it into a single vector containing all the elements.S←...
assigns this new vector to vectorS
.⍴S
gives us the length of that list.?
is the random operator, so?⍴S
gives us a random number between 0 and the length of the list (exclusive) (this is why it relies on⎕IO
being 0; otherwise it's between 1 and the length, inclusive).S[...]
returns the element at the given index.quelle
Q
, since you never use it. And IIRC you can remove the newline before the del (little triangle-thing marking the end of the function.)<IO> <IO>⍉
to get the main diagonal was even a thing!MATLAB, 30 bytes
This assumes MATLAB R2015a or newer and with the Statistics & Machine Learning toolbox installed.
See the explanation below for how
repelem
is used. The difference between this shorter one and the one below is that the S&ML toolbox includes the functiondatasample
which can be used to take one or more elements from an array at random (with uniform probability) which allows an anonymous function to be used, stripping away theinput/disp
calls.MATLAB, 49 bytes
This code assumes that MATLAB R2015a or newer is used as that is when the
repelem
function was introduced.repelem
is a function which takes two parameters, the first is an array of numbers to be replicated, and the second is an array of how many times the corresponding element should be replicated. Essentially the function performs run-length decoding by providing the number and the run-length.By providing the same input to both inputs of
repelem
we end up with an array which consists of n times more of element n if that makes sense. If you provided[1 2 3]
you would get[1 2 2 3 3 3]
. If you provided[1 2 4 2]
you would get[1 2 2 4 4 4 4 2 2]
. By doing this it means that if we select an element with uniform probability (randi(m)
gives a random integer from 1 to m with uniform probability), each element n has an n times higher probability of being selected. In the first example of[1 2 3]
,1
would have a 1/6 chance,2
would have a 2/6 chance and3
would have a 3/6 chance.As a side note, because
repelem
is not available yet for Octave, I can't give a TIO link. Additionally because Octave can't be used there is a big character penalty asinput()
anddisp()
need to be used as an anonymous function is not possible. If Octave supportedrepelem
, the following could be used:That would have saved 16 bytes, but it was not to be.
quelle