Biased random?

J. Cliff Dyer jcd at sdf.lonestar.org
Mon Aug 27 18:23:11 EDT 2007


I don't know if this is pythonic or not, but try something like this:

from math import log
from random import randint
def: skewedrandom(n):
    int(log(randrange(1,n), 2))

Play with your log to get the range you want

Cheers,
Cliff

Grant Edwards wrote:
> On 2007-08-27, Jun-geun Park <junkeun.park at gmail.com> wrote:
>
>   
>>> I have a list of items, and need to choose several elements
>>> from it, "almost random". The catch is that the elements from
>>> the beginning should have more chance of being selected than
>>> those at the end (how much more? I don't care how the
>>> "envelope" of probability looks like at this point - can be
>>> linear). I see that there are several functions in Python
>>> standard libraries for various distribution, but is there an
>>> easy pythonic way to make them do what I need?
>>>       
>
>   
>> That's weird. random.randint(a,b) will be enough for most
>> cases. Test your system to see the distribution is uniform
>> with something like:
>>     
>
> Except he wants a non-uniform distribution.
>
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070827/12a3e00f/attachment.html>


More information about the Python-list mailing list