-1
box_content=[[[1,2,4],[4,5,1]],[[2,3,1],[2,3,1]]]

def pop_a_message(box_content):
    send_message_index = random.randrange(len(box_content))
    receive_message_index = random.randrange(len(box_content) - 1) 
    if send_message_index >= receive_message_index: receive letter += 1    
    friend_message= random.randrange(len(send_message[0]))

Can I change this function so that from the send_message that will be selected, a particular friend_message (for example the friend_message in index [0]) will appear more often than the others ?

Paolo
  • 17,649
  • 21
  • 81
  • 115
user1714768
  • 27
  • 1
  • 4

1 Answers1

1

I think your question might be similar to this Generate random numbers with a given (numerical) distribution

So as Sven Marnach is suggesting:

scipy.stats.rv_discrete might be what you want. You can supply your probabilities via the values parameter. You can then use the rvs() method of the distribution object to generate random numbers.

if you apply this to your list l (suppose you want to get 56 50% of the time):

from scipy.stats import rv_discrete
l=[2,4,56,9]
vals = [l, (0.1, 0.2, 0.5, 0.2)]
custm = rv_discrete(name='custm', values=vals)
for i in l:
    yield generic.rvs(custm)

Not sure about the last line, but if you read the documentation, you will find out the proper way.

Community
  • 1
  • 1
davekr
  • 2,256
  • 1
  • 24
  • 32