Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.05; 'elements.': 0.05; 'initialize': 0.05; 'memory.': 0.05; 'expected.': 0.09; 'extern': 0.09; 'modulo': 0.09; 'python': 0.11; 'def': 0.14; 'declaration': 0.16; 'declaration,': 0.16; 'subject:Cython': 0.16; 'threw': 0.16; 'wrote:': 0.16; 'thanks,': 0.19; 'from:addr:chris': 0.22; 'space.': 0.22; 'am,': 0.23; 'code.': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'figure': 0.27; 'updating': 0.27; 'initialized': 0.29; 'received:dreamhost.com': 0.29; 'received:g.dreamhost.com': 0.29; 'array': 0.29; 'convert': 0.29; 'correct': 0.29; 'function': 0.30; 'received:10.0.0': 0.32; 'problem': 0.33; 'int': 0.33; 'definition': 0.34; 'received:10.0': 0.34; "i'll": 0.34; 'to:addr:python-list': 0.35; 'but': 0.36; 'there': 0.36; 'received:10': 0.37; 'subject:: ': 0.37; "won't": 0.38; 'systems,': 0.38; 'version': 0.38; 'enough': 0.39; 'unable': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'some': 0.40; 'here.': 0.61; 'times': 0.61; 'skip:u 10': 0.62; 'high': 0.62; 'more': 0.62; 'total': 0.64; 'charset:windows-1252': 0.65; 'results': 0.66; 'subject:. ': 0.66; 'x):': 0.84; 'indicator': 0.91 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=cdreimer.com; h=message-id :date:from:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; s=cdreimer.com; bh=VKQo gTaVvlEUJKr9GEH/0QcnvKU=; b=FDsWaJOkT/Bs+mDJVdNdMyfmO8yKnkje3syq fpTVBOdH54DpdvH1QRvXRhN3uykyNWXDrPmXk8rrRjOqLtWqeol51Jy4v87Wob/5 0DHgqySQS5/8PqjO2bwd2aKWV6OzhVM0sWs1kq15wEewG1Gpddax0t9boD2WG9cB bxeVm4c= Date: Sun, 07 Jun 2015 11:40:37 -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: Re: Python Random vs. Cython C Rand for Dice Rolls References: <55747C96.7090006@cdreimer.com> In-Reply-To: Content-Type: text/plain; charset=windows-1252; 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: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433702448 news.xs4all.nl 2907 [2001:888:2000:d::a6]:42282 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92287 On 6/7/2015 10:33 AM, Chris Angelico wrote: > The negative result is a strong indicator that you're not seeing the > results of rand() here. While there is a potential for bias (check out > RAND_MAX, and consider that there may be some small bias there; > although on most modern systems, RAND_MAX is going to be high enough > that the bias from modulo 6 won't be highly visible), it's much MUCH > more likely that you're looking at uninitialized memory. Here's the revised Cython code. cdef extern from "stdlib.h": int c_libc_rand "rand"() cdef int f[12] def roll(int x): cdef: int a = 0, b = 0, i = 0 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 new console output. PS Z:\projects\programming\python\basic_games\fastdice> python test_fastdice.py TOTAL SPOTS NUMBER OF TIMES 2 1389911 3 2777722 4 4168248 5 5553632 6 6944907 7 8334670 8 6945597 9 5553557 10 4167485 11 2775806 12 1388465 1.65599989891 I had to put the array definition outside of the function declaration, where it's automatically initialized to zero in the global space. I tried to initialize the array from inside the function but Cython threw up errors, either the C declaration wasn't correct or unable to convert from C to Python when updating the array elements. I'll figure out that problem later. The new version works as expected. Thanks, Chris R.