Numpy.random.choice weights help

Hey so basically im trying to make a list of random weights for numpy.random.choice depending on how many items are in that list but the only function I can think of is x = x*.5 for example

weight = 1
l = []
weights = []
ans = []

for x in range(100):

      l.append(x)

for x in range(len(l)):

      weight = weight * .5
      weights.append(weight)

for x in range(10):

       b = numpy.random.choice(l,1,weights)
       ans.append(b)

print(ans)

^That works but if I change the multiplication value in weights (weight = weight *.5) it doesn’t work and I can’t think of another function that equals one, if you have any suggestions that would be great, thanks

If you just want a list of random numbers from 0 to 1, then you can use random.random() for each element of the list, like so:

weights = [numpy.random.random() for _ in range(100)]

which is equivalent to:

weights = []
for _ in range(100):
    weights.append(numpy.random.random())
2 Likes

Im more so looking for a weighted list

Oh okay, I understand now. What criteria are you thinking of for the weights? What you used earlier isn’t even working. I wrote this tiny piece of code to prove it:

import random
import numpy
weight = 1
l = []
ans = []
weights = []
myDict = {}

for i in range(100):
    myDict[i] = 0
    l.append(i)
for x in range(len(l)):
    weight = weight * 0.5
    weights.append(weight)
for _ in range(1000):
    for x in range(10):
        b = numpy.random.choice(l, 1, weights)
        myDict[b[0]] += 1
        ans.append(b)
print(myDict)
# {0: 101, 1: 88, 2: 96, 3: 98, 4: 102, 5: 103, 6: 100, 7: 80, 8: 89, 9: 91, 10: 118, 11: 95, 12: 99, 13: 123, 14: 88, 15: 98, 16: 101, 17: 100, 18: 90, 19: 121, 20: 104, 21: 102, 22: 109, 23: 107, 24: 90, 25: 102, 26: 97, 27: 78, 28: 108, 29: 95, 30: 98, 31: 88, 32: 94, 33: 91, 34: 105, 35: 92, 36: 95, 37: 89, 38: 105, 39: 107, 40: 117, 41: 100, 42: 87, 43: 102, 44: 100, 45: 83, 46: 105, 47: 96, 48: 101, 49: 102, 50: 114, 51: 106, 52: 103, 53: 96, 54: 100, 55: 115, 56: 108, 57: 106, 58: 96, 59: 109, 60: 92, 61: 113, 62: 115, 63: 100, 64: 107, 65: 85, 66: 96, 67: 95, 68: 103, 69: 103, 70: 123, 71: 116, 72: 105, 73: 102, 74: 85, 75: 99, 76: 104, 77: 75, 78: 121, 79: 117, 80: 87, 81: 96, 82: 94, 83: 76, 84: 111, 85: 98, 86: 103, 87: 89, 88: 91, 89: 106, 90: 99, 91: 85, 92: 98, 93: 106, 94: 107, 95: 98, 96: 103, 97: 101, 98: 102, 99: 111}
# this means that out of 10'000 theoretically non-random draws,
# each number seems to have been chosen about 100 times out of 10'000, or 1/100.
# considering there are 100 numbers in total, 
# there is no difference between this and a simple random.randint(0, 100)

Well my idea is, basically im making a text based adventure and I need a random function for coin drops but I want the tendency to be fewer coins however weight * 0.5 makes the weight to small to quickly so I’m just asking for another equation if you had one

I see. You could use something like 100*n/(x+100), substituting any number instead of 100

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.