Does shuffle() produce uniform result ?

tooru honda tooru_honda at fast-mail.org
Tue Aug 28 04:27:12 EDT 2007


Thanks to everyone who replied, (and special thanks to Alex Martelli,) I 
was able to accomplish what I originally asked for: a shuffle() which is 
both fast and without bias.  It is the first time I try to optimize 
python code, and it is definitely a very rewarding experience.


To bring closure to this thread, I will provide below some statistics 
obtained from cProfile:


1. using random.SystemRandom

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)

  4182879   35.154    0.000  211.006    0.000 os.py:724(urandom)
  4182879   31.981    0.000  248.291    0.000 random.py:767(random)
    40578   17.977    0.000  266.300    0.007 random.py:250(shuffle)
  4182879   29.487    0.000   29.487    0.000 {posix.close}
  4182879   99.460    0.000   99.460    0.000 {posix.open}
  4182879   41.794    0.000   41.794    0.000 {posix.read}

2. my original optimization

     8268    0.133    0.000    2.577    0.000 os.py:724(urandom)
  4134322   15.094    0.000   21.606    0.000 baccarat.py:70(get2bytes)
  4131441   17.221    0.000   41.722    0.000 baccarat.py:85(randrange)
    40590    7.059    0.000   48.795    0.001 baccarat.py:112(shuffle)

3. using Alex's generator with string

     4117    0.058    0.000    2.048    0.000 os.py:724(urandom)
  4214795   10.186    0.000   14.880    0.000 baccarat.py:93(rand2)
  4211790    8.883    0.000   23.763    0.000 baccarat.py:106(randrange)
    40739    6.101    0.000   29.878    0.001 baccarat.py:131(shuffle)


4. using Alex's generator with array

     2081    0.029    0.000    1.736    0.001 os.py:724(urandom)
  4161569    1.724    0.000    3.473    0.000 baccarat.py:100(rand2new)
  4158474    8.315    0.000   11.788    0.000 baccarat.py:113(randrange)
    40514    5.726    0.000   17.528    0.000 baccarat.py:138(shuffle)



More information about the Python-list mailing list