Biased random?

Mark Dickinson dickinsm at gmail.com
Thu Aug 30 16:36:20 EDT 2007


On Aug 30, 3:55 pm, Ivan Voras <ivoras at __fer.hr__> wrote:
> I've noticed something interesting in my test: the value 0 appears less
> often than other values (which behave as they should).

That's because the call to abs() usually collapses two values to one
(e.g. -2 and 2 both end up being 2),
but there's only one integer n for which abs(n) == 0.

One possible fix:  do

  x = randint(0, 10) - randint(0, 10)
  x = abs(x) - (x<0)

This collapses -1 and 0 to 0, -2 and 1 to 1, etc.

Mark




More information about the Python-list mailing list