Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92275

Python Random vs. Cython C Rand for Dice Rolls

Date 2015-06-07 10:17 -0700
From "C.D. Reimer" <chris@cdreimer.com>
Subject Python Random vs. Cython C Rand for Dice Rolls
Newsgroups comp.lang.python
Message-ID <mailman.262.1433697446.13271.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Python Random vs. Cython C Rand for Dice Rolls "C.D. Reimer" <chris@cdreimer.com> - 2015-06-07 10:17 -0700
  Re: Python Random vs. Cython C Rand for Dice Rolls Steven D'Aprano <steve@pearwood.info> - 2015-06-08 04:33 +1000
    Re: Python Random vs. Cython C Rand for Dice Rolls "C.D. Reimer" <chris@cdreimer.com> - 2015-06-07 11:56 -0700
    Re: Python Random vs. Cython C Rand for Dice Rolls Laura Creighton <lac@openend.se> - 2015-06-08 19:33 +0200
    Re: Python Random vs. Cython C Rand for Dice Rolls Sturla Molden <sturla.molden@gmail.com> - 2015-06-08 23:44 +0200

csiph-web