Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been experimenting with python and trying out things and currently i was wondering why this happens in random choices

random.choices([0,1,5,100], weights=[0,0,0,0])

it always chooses the last item in th list (100). I know in reality if all have zero possibilities then you don't get anything but in here it picks the last one specifically and i was wondering why and can anyone explain this.

What I have tried:

tried looking at the random code but i can't really understand it sorry im currently an amateur
Posted
Updated 25-Nov-18 1:59am
v2
Comments
Richard MacCutchan 25-Nov-18 5:23am    
I cannot find any reference to choices() in Python's documentation for random.

Sorry, just found it in v 3.6.
xStahart 25-Nov-18 5:27am    
ow yea, didn't notice it was particularly new i guess.
xStahart 25-Nov-18 12:25pm    
Just to clarify things, i am just wondering why it outputs the last item if you enter their weights value of all 0 each..

I just tried this:
Python
for i in range(10):
	random.choices([0,1,5,100])

and got the following result:
[5]
[0]
[0]
[0]
[100]
[5]
[100]
[100]
[0]
[5]

It seems that using weights of all zeroes affects the outcome.
 
Share this answer
 
Comments
xStahart 25-Nov-18 12:23pm    
Yea, my question is that why if inputed with the weights of all 0, it outputs the last item(in your case if you add it it would be 100)
Richard MacCutchan 25-Nov-18 12:47pm    
Yes, and I got the same result. I am guessing that weights of zero for each element actually mess up the weighting system, and should not be used; largely because it makes no sense.
It doesn't: firstly because it's not random.choices it's random.choice, and secondly because of statistics:
import random
print(random.choice([0,1,2,3]))
print(random.choice([0,1,2,3]))
print(random.choice([0,1,2,3]))
print(random.choice([0,1,2,3]))
print(random.choice([0,1,2,3]))
print(random.choice([0,1,2,3]))
print(random.choice([0,1,2,3]))
print(random.choice([0,1,2,3]))
print(random.choice([0,1,2,3]))
print(random.choice([0,1,2,3]))
Gives me
1
0
3
1
1
1
1
3
2
2
Then this:
2
0
2
0
0
2
1
0
2
1
Notice that the first test has a "run" of '1's - the smaller your sample, the more likely a "run" is: with only four elements to choose from, it's quite possible that you will get the same value several times in a row. Run it a thousand times, and the count's will even out, but in the short term you should expect "odd looking" results.
 
Share this answer
 
Comments
xStahart 25-Nov-18 3:37am    
No, im talking really talking about random.choices where you can input the possibilities which is called weights.

random.choices(population, weights=None, *, cum_weights=None, k=1)

then if you put weights with all 0 possibilities then it does what i said above. already tried like a couple of times.
xStahart 25-Nov-18 3:39am    
Sorry for the misleading title, forgot to add s
markkuk 25-Nov-18 6:42am    
random.choices is a new function added to Python 3.6 (and later)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900