1
import random

number=list(range(1,10))
weighted=[1]*2+[2]*2+[3]*2+[4]*2+[5]*2
number_weighted=random.choice(number,weighted,k=1)  **#if k=4 then the same number is chosen sometimes**

I want to use loop 3 times to choose the each one number.

I want to choose the number that independent(not same), weighted.

In python, if you know this problem, I would appreciate it if you taught me

For example, number=[1,2,3,4,5] weighted=[0.1,0.1,0.4,0.3,0.1] then choose two number i want 3, 4 Probability) but random.choice function is sometimes 1,1 selected.

so, i think i take one number (suppose number 3) then number=[1,2,4,5] weighted=[0.1,0.1,0.3,0.1] and i take one number (suppose number 4). use loop function

jossjoss
  • 13
  • 3
  • if k=1 you want to choose between the Numbers with 1 weight? – Atreyagaurav Jan 01 '20 at 04:57
  • So k is how many times the number is chosen with replacement? [Related to your quesion](https://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice) – DarrylG Jan 01 '20 at 05:03
  • For weights Python 3.6 introduced [random.choices](https://docs.python.org/3/library/random.html) or you can use [numpy.random.choice](https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.choice.html) (which doesn't need Python 3.6). – DarrylG Jan 01 '20 at 05:10

1 Answers1

0

Your question isn't quite clear, so comment if it doesn't solve your problem. Define functionn which returns random from the list and weight. another function to make sure you have n randoms from different weights.

And your weight and array was of different length I hope that was an error.

import random

def get_rand(num_list,weight_list,weight):
    selection_from= [i for i,v in enumerate(weight_list) if v==weight]
    print(selection_from)
    rand_index =random.choice(selection_from)
    return num_list[rand_index]

def get_n_rand(num_list,weight_list,n):
    weights= list(set(weight_list))
    random.shuffle(weights)
    final_list=[]
    # if you don't want numbers from same weight
    for weight in weights[:n]:
        final_list.append(get_rand(num_list,weight_list,weight))
    #if same weight is also fine use this:
    #for i in range(n):
    #    weight = random.choice(weights)
    #    final_list.append(get_rand(num_list,weight_list,weight))
    return final_list


number=list(range(1,10))
weighted=[1]*2+[2]*2+[3]*2+[4]*2+[5]*1
assert(len(number)==len(weighted))
rand=get_n_rand(number,weighted,3)
print("selected numbers:",rand)
print("their weights:",[weighted[number.index(i)] for i in rand])

Since you had hard time understanding,

selection_from= [i for i,v in enumerate(weight_list) if v==weight]

is equivalent to:

selection_from= []
for i in range(len(weight_list)):
   v= weight_list[i]
   if v==weight:
      selection_from.append(i)
Atreyagaurav
  • 1,145
  • 6
  • 15
  • My opinion is, for example 1(weight=5), 2(weight=1), 3(weight=1), 4(weight=2), then if i choose from two number then i want to 1 and 4 (probability), but this state {number_weighted=random.choice(number,weighted,k=2) } sometimes 1 and 1 – jossjoss Jan 01 '20 at 05:52
  • i still don't get exactly what you mean, but this gives you the numbers with different weights. – Atreyagaurav Jan 01 '20 at 05:57
  • I have added a commented block, if you want possibilities of repetition of weights then use that loop instead. – Atreyagaurav Jan 01 '20 at 06:01
  • number=[1,2,3,4,5] weight=[0.1,0.1,0.4,0.3,0.1] i think i take one number (suppose number 3) then number=[1,2,4,5] weighted=[0.1,0.1,0.3,0.1] and i take one number (suppose number 4). use loop function – jossjoss Jan 01 '20 at 06:07
  • i don't understand ```[i for i,v in enumerate(weight_list) if v==weight]``` what mean? – jossjoss Jan 01 '20 at 07:00
  • enumerate(list) will give you index,value pair for looping. if v==weight will make sure it only selects the index from that weight. this overall gives you the list of index in weight_list where the weight is your selected value – Atreyagaurav Jan 01 '20 at 07:03
  • is it right? [i //for i,v in enumerate(weight_list)// if v==weight] i don't know``` i for i``` – jossjoss Jan 01 '20 at 07:32
  • what are you typing? extra // and ``` making it unreadable, other than that you are typing the same thing. – Atreyagaurav Jan 01 '20 at 07:34
  • (i for i )this section, i don't know – jossjoss Jan 01 '20 at 07:36
  • that's not i for i, it's i for (i,v), once do `print(list(enumerate(x)))` with any list `x`, you'll see what i,v means. that's same i,v in the edit I provided at end. – Atreyagaurav Jan 01 '20 at 07:38