Unsorting(randomizing) a sequence
Tim Peters
tim_one at email.msn.com
Wed Aug 18 02:04:18 CEST 1999
[Jeremy Hylton]
> I've rather fond of the following approach:
>
> import random
>
> def randomize(a, b):
> return random.choice([-1, 0, 1])
> ...
> No idea how fast this is, though not very I expect.
def test(N):
d = {}
for i in xrange(N * 6):
x = range(3)
x.sort(randomize)
t = tuple(x)
d[t] = d.get(t, 0) + 1
print N, d
test(1000)
1000 {(2, 1, 0): 68,
(0, 2, 1): 625,
(0, 1, 2): 4443,
(2, 0, 1): 289,
(1, 2, 0): 155,
(1, 0, 2): 420}
With a chi square busting distribution like that, I'm not sure speed is the
first thing you should be worried about <wink>.
a-random-decision-may-not-yield-a-random-outcome-ly y'rs - tim
More information about the Python-list
mailing list