Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92278
| Path | csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'memory.': 0.05; 'sys': 0.05; '#include': 0.07; '(especially': 0.07; 'main()': 0.07; '++i)': 0.09; 'modulo': 0.09; 'toss': 0.09; 'cc:addr:python-list': 0.10; 'python': 0.11; 'weird': 0.15; '"int': 0.16; 'as-is': 0.16; 'correctness.': 0.16; 'declaration': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'middle,': 0.16; 'peak': 0.16; 'subject:Cython': 0.16; 'worst': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'produces': 0.22; 'void': 0.22; 'am,': 0.23; '2015': 0.23; 'replacing': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'not.': 0.27; 'raw': 0.27; 'least': 0.27; 'message- id:@mail.gmail.com': 0.28; "i'm": 0.29; 'curve': 0.29; 'initialized': 0.29; 'loop,': 0.29; 'random': 0.29; "i'd": 0.31; 'similar': 0.32; 'int': 0.33; 'case,': 0.34; 'add': 0.34; 'received:google.com': 0.34; 'done': 0.35; 'but': 0.36; 'text': 0.36; 'there': 0.36; 'basic': 0.36; 'subject:: ': 0.37; "won't": 0.38; 'systems,': 0.38; 'enough': 0.39; 'some': 0.40; 'your': 0.60; 'real': 0.61; 'here.': 0.61; 'skip:u 10': 0.62; 'high': 0.62; 'more': 0.62; 'day.': 0.63; 'results': 0.66; 'subject:. ': 0.66; 'low': 0.83; 'chrisa': 0.84; 'reminds': 0.84; 'to:none': 0.90; 'indicator': 0.91; 'yours.': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Vs6DkAyOlPzULErkh3YUWWN1nH23cIjf0g7X4Wcx81Q=; b=JuOJK8Q3uHTYHd4WDe8/+ppK/bXWRAgdruQG9JY0YdI2yex/POGMaponxqt+DU1bEG 7k8dsTF9i8f4KYxt8stPO1JD/342MTx2fCJZndyRbh7XlBKZSO3DNfRZDx5aau6n1Xaw q+i3d4Q0aAINF5z8e+Swhyf8pBWU9Lc7Ea8vESCzOjemVQF00JrUKjDr6d6s6lxI4Xwq oWhxt79CYBliGRK2yNqzdVyNzNIBbBAyjNGjGviqAl25cEZFFbUM0hudLr3SLPBbozt8 rhCoD6XBxY/rjFrc3pgoe+Wi1CWL15OyX94lU04/9i8LMzYblS59lXsfMvW/Yh3GQQFw 3oEw== |
| MIME-Version | 1.0 |
| X-Received | by 10.107.131.196 with SMTP id n65mr15574288ioi.53.1433698380372; Sun, 07 Jun 2015 10:33:00 -0700 (PDT) |
| In-Reply-To | <55747C96.7090006@cdreimer.com> |
| References | <55747C96.7090006@cdreimer.com> |
| Date | Mon, 8 Jun 2015 03:33:00 +1000 |
| Subject | Re: Python Random vs. Cython C Rand for Dice Rolls |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.265.1433698383.13271.python-list@python.org> (permalink) |
| Lines | 83 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1433698383 news.xs4all.nl 2922 [2001:888:2000:d::a6]:46264 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:92278 |
Show key headers only | View raw
On Mon, Jun 8, 2015 at 3:17 AM, C.D. Reimer <chris@cdreimer.com> wrote:
> 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.
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. I'm not
overly familiar with Cython, but can you simply add "={0}" to the
declaration to force it to be initialized to all zeroes? That's what
I'd do in raw C. Here's the same thing done in straight-forward C:
#include <stdlib.h>
#include <stdio.h>
void roll(int *f,int x)
{
int i, a, b;
for (i=0; i<x; ++i)
{
a = rand() % 6 + 1;
b = rand() % 6 + 1;
f[(a + b) - 1] += 1;
}
}
int main()
{
int f[12];
int i;
roll(f, 50000000);
for (i=1;i<12;++i)
printf("%-15d %d\n", i+1, f[i]);
return 0;
}
rosuav@sikorsky:~$ time ./a.out
2 1387782
3 6972536
4 4170215
5 21326281
6 6945600
7 8334121
8 6943983
9 9750611
10 4168554
11 6972254
12 1389371
real 0m1.089s
user 0m1.088s
sys 0m0.000s
Similar weird results to yours. Replacing "int f[12];" with "int
f[12]={0};" produces a much more plausible bell curve:
rosuav@sikorsky:~$ time ./a.out
2 1387782
3 2776523
4 4170215
5 5552330
6 6945600
7 8334121
8 6943983
9 5554675
10 4168554
11 2776846
12 1389371
real 0m1.093s
user 0m1.088s
sys 0m0.000s
But I don't know Cython, so I don't know whether this would work as-is
or not. Worst case, toss in a quick initialize-to-zero loop above your
main loop, and you can at least verify correctness.
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Python Random vs. Cython C Rand for Dice Rolls Chris Angelico <rosuav@gmail.com> - 2015-06-08 03:33 +1000
csiph-web