playing sound in mac osx
Hi, I am a newbie to mac and python. Is there an easy way to play wav or mp3 sound file ? I used to use winsound module before switching to mac, but that only works for windows. Thanks Tooru P.S. I am using Mac OSX 10.4.8 and Python 2.5 -- http://mail.python.org/mailman/listinfo/python-list
Does shuffle() produce uniform result ?
Hi, I have read the source code of the built-in random module, random.py. After also reading Wiki article on Knuth Shuffle algorithm, I wonder if the shuffle method implemented in random.py produces results with modulo bias. The reasoning is as follows: Because the method random() only produces finitely many possible results, we get modulo bias when the number of possible results is not divisible by the size of the shuffled list. 1. Does shuffle() produce uniform result ? 2. If not, is there a fast and uniform shuffle() available somewhere ? Thanks ! -tooru honda -- http://mail.python.org/mailman/listinfo/python-list
Re: Does shuffle() produce uniform result ?
Hi, First of all, my thanks to all of you who replied. I am writing a gamble simulation to convince my friend that his "winning strategy" doesn't work. I use shuffle method from a random.SystemRandom instance to shuffle 8 decks of cards. As the number of cards is quite small (number of cards is 416), the nonuniformity doesn't matter as most of you have already said. Just to avoid argument from my friend, I am considering writing my own randint and shuffle methods based on os.urandom() though. -tooru honda -- http://mail.python.org/mailman/listinfo/python-list
Re: Does shuffle() produce uniform result ?
At the end, I think it is worthwhile to implement my own shuffle and random methods based on os.urandom. Not only does the resulting code gets rid of the minuscule bias, but the program also runs much faster. When using random.SystemRandom.shuffle, posix.open and posix.close from calling os.urandom account for almost half of the total execution time for my program. By implementing my own random and getting a much larger chunk of random bytes from os.urandom each time, I am able to reduce the total execution time by half. -tooru honda P.S. I use python 2.5.1 on MacOSX 10.4.10 (PowerPC). -- http://mail.python.org/mailman/listinfo/python-list
Re: Does shuffle() produce uniform result ?
By incorporating Alex's code, I got another performance boost of 20%. It is mostly due to Alex's more efficient implementation of block random than my own version. -tooru honda Below is the code I have now: from binascii import hexlify from os import urandom class rcRandomC(random.SystemRandom): def __init__(self): random.SystemRandom.__init__(self) def rand2(): while True: randata = urandom(2*1024) for i in xrange(0, 2*1024, 2): yield int(hexlify(randata[i:i+2]),16)# integer in [0,65535] self.rand2_M = rand2().next # modified from random._randbelow def randrange(self,startN,stopN): """Choose a random integer from range(startN, stopN). widthN<=65536 """ widthN=stopN-startN left_over_N=65536%widthN upper_bound_N= 65535-left_over_N random_number=self.rand2_M() while random_number>upper_bound_N: random_number=self.rand2_M() r = random_number%widthN return startN+r def shuffle(self, x): """x, random=random.random -> shuffle list x in place; return None. """ randrange=self.randrange for i in reversed(xrange(1, len(x))): # pick an element in x[:i+1] with which to exchange x[i] j = randrange(0,i+1) x[i], x[j] = x[j], x[i] -- http://mail.python.org/mailman/listinfo/python-list
Re: Does shuffle() produce uniform result ?
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.1540.000 211.0060.000 os.py:724(urandom) 4182879 31.9810.000 248.2910.000 random.py:767(random) 40578 17.9770.000 266.3000.007 random.py:250(shuffle) 4182879 29.4870.000 29.4870.000 {posix.close} 4182879 99.4600.000 99.4600.000 {posix.open} 4182879 41.7940.000 41.7940.000 {posix.read} 2. my original optimization 82680.1330.0002.5770.000 os.py:724(urandom) 4134322 15.0940.000 21.6060.000 baccarat.py:70(get2bytes) 4131441 17.2210.000 41.7220.000 baccarat.py:85(randrange) 405907.0590.000 48.7950.001 baccarat.py:112(shuffle) 3. using Alex's generator with string 41170.0580.0002.0480.000 os.py:724(urandom) 4214795 10.1860.000 14.8800.000 baccarat.py:93(rand2) 42117908.8830.000 23.7630.000 baccarat.py:106(randrange) 407396.1010.000 29.8780.001 baccarat.py:131(shuffle) 4. using Alex's generator with array 20810.0290.0001.7360.001 os.py:724(urandom) 41615691.7240.0003.4730.000 baccarat.py:100(rand2new) 41584748.3150.000 11.7880.000 baccarat.py:113(randrange) 405145.7260.000 17.5280.000 baccarat.py:138(shuffle) -- http://mail.python.org/mailman/listinfo/python-list