Path: csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'exercise': 0.03; 'subject:Python': 0.05; '(especially': 0.07; '[0]': 0.07; 'difference,': 0.07; 'scripts': 0.09; 'extern': 0.09; 'imported': 0.09; 'statements': 0.09; 'python': 0.11; 'python.': 0.11; 'def': 0.14; '1),': 0.16; 'middle,': 0.16; 'peak': 0.16; 'stripped': 0.16; 'subject:Cython': 0.16; 'time.time()': 0.16; 'creates': 0.18; 'thanks,': 0.19; 'delay': 0.22; 'from:addr:chris': 0.22; "i've": 0.24; 'import': 0.24; '(this': 0.24; 'script': 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'mostly': 0.27; 'curve': 0.29; 'received:dreamhost.com': 0.29; 'received:g.dreamhost.com': 0.29; 'random': 0.29; 'print': 0.31; 'seconds': 0.31; 'code': 0.31; 'similar': 0.32; 'received:10.0.0': 0.32; 'int': 0.33; 'received:10.0': 0.34; 'to:addr:python-list': 0.35; 'besides': 0.35; 'something': 0.35; 'text': 0.36; 'url:org': 0.36; 'there': 0.36; 'basic': 0.36; 'quite': 0.37; 'received:10': 0.37; 'version': 0.38; 'test': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'times': 0.61; 'day.': 0.63; 'total': 0.64; 'here': 0.66; 'results': 0.66; 'subject:. ': 0.66; 'obvious': 0.72; 'url:page': 0.72; 'attention': 0.75; 'low': 0.83; 'childhood': 0.84; 'noticeable': 0.84; 'random,': 0.84; 'reminds': 0.84; 'x):': 0.84; 'url:php': 0.86; 'different.': 0.91 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=cdreimer.com; h=message-id :date:from:mime-version:to:subject:content-type: content-transfer-encoding; s=cdreimer.com; bh=sXdogjbs7iBeEO83Iq WnoDg6D+g=; b=FnvwjM4lrLWqFlNFtBYmX4CMuKPZ4kLcohjrgGdpittpGqfXN4 CKvhiQW13hcI2sRHUGdHIrgsXB7uY5kmiDv9LrNcfH4TYnxQn5LASHEjHOvL7XbW NjsIvC4ZpaM95qm0XfuI6VNlFEWcyYD7tzYUr1pGDCDWcExWzpUu/8/G8= Date: Sun, 07 Jun 2015 10:17:10 -0700 From: "C.D. Reimer" User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Python Random vs. Cython C Rand for Dice Rolls Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 116 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433697446 news.xs4all.nl 2835 [2001:888:2000:d::a6]:37870 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92275 Greetings, I've revisited my misbegotten childhood by translating the programs from "BASIC Computer Games" by David H. Ahl into Python. This is mostly an exercise in unraveling spaghetti code with all those GOTO statements going all over the place. The dice program caught my attention in particular for a stripped down version in Python and Cython to test the random number generator functionality. http://www.atariarchives.org/basicgames/showpage.php?page=57 Here are my scripts to roll a pair of dice 50,000,000 times (this number creates a noticeable delay on my 3GHz quad processor). The Python script uses random, the Cython script uses C rand. Besides the obvious speed difference, the results are quite different. This is the Python script that takes ~197 seconds to complete. import random, time startTime = time.time() f = [0] * 12 for i in range(50000000): a = random.randint(1,6) b = random.randint(1,6) f[(a + b) - 1] += 1 print "\nTOTAL SPOTS","\tNUMBER OF TIMES\n" for i in range(1,12): print ' ' + str(i + 1), '\t\t ', f[i] print '\n', time.time() - startTime This the Cython script that is imported into a test scripts similar to the Python script that takes ~1.6 seconds to complete. cdef extern from "stdlib.h": int c_libc_rand "rand"() def roll(int x): cdef: int a, b, i int f[12] for i in range(x): a = c_libc_rand() % 6 + 1 b = c_libc_rand() % 6 + 1 f[(a + b) - 1] += 1 return f Here's the console output. PS Z:\projects\programming\python\basic_games\fastdice> python slowdice.py TOTAL SPOTS NUMBER OF TIMES 2 1388086 3 2776286 4 4165556 5 5555869 6 6940547 7 8335864 8 6945446 9 5556470 10 4169549 11 2777972 12 1388355 197.006999969 PS Z:\projects\programming\python\basic_games\fastdice> python test_fastdice.py TOTAL SPOTS NUMBER OF TIMES 2 1389911 3 -2144697697 4 4168249 5 35008856 6 6944907 7 512318212 8 6945597 9 342017362 10 4167485 11 2775806 12 1388465 1.63799977303 The Python random shows a uniform bell curve with low numbers at the ends and the peak in the middle, which is similar to the text in the book for the BASIC program. The Cython C rand is over all the place (especially with a negative number), which reminds me how bad the random number generator was on my 1MHz C64 in the day. Is there something in the Cython code that I need to change and/or find a better C random number generator? Thanks, Chris R.