Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92275 > unrolled thread
| Started by | "C.D. Reimer" <chris@cdreimer.com> |
|---|---|
| First post | 2015-06-07 10:17 -0700 |
| Last post | 2015-06-08 23:44 +0200 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | "C.D. Reimer" <chris@cdreimer.com> |
|---|---|
| Date | 2015-06-07 10:17 -0700 |
| Subject | Python Random vs. Cython C Rand for Dice Rolls |
| Message-ID | <mailman.262.1433697446.13271.python-list@python.org> |
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.
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2015-06-08 04:33 +1000 |
| Message-ID | <55748e6d$0$13008$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #92275 |
On Mon, 8 Jun 2015 03:17 am, C.D. Reimer wrote: > 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. C rand is not even close to random. The technical term for it is "shite". -- Steve
[toc] | [prev] | [next] | [standalone]
| From | "C.D. Reimer" <chris@cdreimer.com> |
|---|---|
| Date | 2015-06-07 11:56 -0700 |
| Message-ID | <mailman.273.1433703386.13271.python-list@python.org> |
| In reply to | #92285 |
On 6/7/2015 11:33 AM, Steven D'Aprano wrote: > C rand is not even close to random. The technical term for it is "shite". Looking through the BASIC book, I remembered all the tricks needed to get a half-way decent number generator on a 1MHz processor back in the day. Either the numbers start repeating after a while or get stuck on a particular number. That's one of the challenges of converting these old games from BASIC to Python. A lot of the stuff done in BASIC was meant to get around the hardware limitations of the early computers. Chris R.
[toc] | [prev] | [next] | [standalone]
| From | Laura Creighton <lac@openend.se> |
|---|---|
| Date | 2015-06-08 19:33 +0200 |
| Message-ID | <mailman.296.1433784812.13271.python-list@python.org> |
| In reply to | #92285 |
Better C random number generator. http://www.pcg-random.org/download.html Laura
[toc] | [prev] | [next] | [standalone]
| From | Sturla Molden <sturla.molden@gmail.com> |
|---|---|
| Date | 2015-06-08 23:44 +0200 |
| Message-ID | <mailman.305.1433799910.13271.python-list@python.org> |
| In reply to | #92285 |
On 08/06/15 19:33, Laura Creighton wrote: > Better C random number generator. > http://www.pcg-random.org/download.html Or for something less minimalistic, just grab randomkit.c and randomkit.h from NumPy, which implements the same Mersenne Twister as Python. That is what I usually do to get fast random numbers from Cython https://github.com/numpy/numpy/tree/master/numpy/random/mtrand Sturla
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web