K
kp87
I am a little bit stuck ....
I want to play a bunch of soundfiles randomly, but i want to give each
soundfile a rating (say 0-100) and have the likelihood that the file be
chosen be tied to its rating so that the higher the rating the more
likely a file is to be chosen. Then i need some additional flags for
repetition, and some other business. I am guessing a dictionary would
be a great way to do this, with the key being the soundfile name and
the values being my ratings and other flags & associated data.
#soundfile name : [rating, %chance it will repeat/loop]
sfiles = { ("sf001") : [85, 15],
("sf002") : [25, 75],
("sf003") : [95, 45],
("sf004") : [35, 95] }
But i am stuck on how to do a random chooser that works according to my
idea of choosing according to rating system. It seems to me to be a bit
different that just choosing a weighted choice like so:
def windex(lst):
'''an attempt to make a random.choose() function that makes weighted
choices
accepts a list of tuples with the item and probability as a pair
like: >>> x = [('one', 0.25), ('two', 0.25), ('three', 0.5)]
>>> y=windex(x)'''
n = random.uniform(0, 1)
for item, weight in lst:
if n < weight:
break
n = n - weight
return item
And i am not sure i want to have to go through what will be hundreds of
sound files and scale their ratings by hand so that they all add up to
100%. I just want to have a long list that i can add too whenever i
want, and assign it a grade/rating according to my whims!
cheers,
-kp---
I want to play a bunch of soundfiles randomly, but i want to give each
soundfile a rating (say 0-100) and have the likelihood that the file be
chosen be tied to its rating so that the higher the rating the more
likely a file is to be chosen. Then i need some additional flags for
repetition, and some other business. I am guessing a dictionary would
be a great way to do this, with the key being the soundfile name and
the values being my ratings and other flags & associated data.
#soundfile name : [rating, %chance it will repeat/loop]
sfiles = { ("sf001") : [85, 15],
("sf002") : [25, 75],
("sf003") : [95, 45],
("sf004") : [35, 95] }
But i am stuck on how to do a random chooser that works according to my
idea of choosing according to rating system. It seems to me to be a bit
different that just choosing a weighted choice like so:
def windex(lst):
'''an attempt to make a random.choose() function that makes weighted
choices
accepts a list of tuples with the item and probability as a pair
like: >>> x = [('one', 0.25), ('two', 0.25), ('three', 0.5)]
>>> y=windex(x)'''
n = random.uniform(0, 1)
for item, weight in lst:
if n < weight:
break
n = n - weight
return item
And i am not sure i want to have to go through what will be hundreds of
sound files and scale their ratings by hand so that they all add up to
100%. I just want to have a long list that i can add too whenever i
want, and assign it a grade/rating according to my whims!
cheers,
-kp---