[Tutor] random distribution question

Andrei Kulakov ak@silmarill.org
Mon, 1 Jul 2002 03:13:53 -0400


Hello tutors,

I have a tough and interesting problem here, I think..

In my player cymbaline, each track has a score from 0 to 100; tracks
with high scores play more often in random mode. There is a setting
called random slope that dictates just how much more often they play.

Here's how it was implemented until now:

runs = 0
while 1:
    n = -5
    for i in range(slope):
	n = random.randrange(n, 101)
    track = random.choice(rand_list)
    if track.score >= n or runs > 50:
	break
    runs += 1

slope was an int from 1 to 5. The higher it is, more likely would
high-scored tracks play, but I'm not too sure just how much more
likely..

Now I'm re-hauling all of the code (most of it is 1-2 years old).

Here's what I came up with:

import random, operator
slope = 1
runs = 0
c = []
for i in range(10):
    while 1:
	n = -5
	n = random.randrange(n, 101)
	left = 100 - n
	cutoff = n + left*slope/11
	c.append(cutoff)
	if runs>50:
	    break
	runs += 1

    csum = reduce(operator.add, c)
    print "avg is ", csum/len(c)

Where slope is from 0 to 10.

To be honest, I have a hard time even stating what the uniform
distribution should be like here. At slope=5, should top 10% of scores
be picked 5 times more often than bottom 10%? I guess at slope=1, we
want the lowest possible difference that'd still be notable to the
user, that would perhaps be 100score be 2 times more likely to be
picked than 0 score, and uniformly increasing from ~1.1 at 1score to
~1.9 at 99score.

At 10slope, let's say 100 times more likely to pick 100score than
0score, uniformly increasing from 0 to 100.

And this relation has to also uniformly increase for 1=>10 slope.

I looked at random module and there's a bunch of distribution functions
but I'm not sure if any of them would apply here.

Anybody got any ideas on this?

Thanks,

 - Andrei

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org