annotate Modules/_randommodule.c @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 6d64cd69f384
children fd25030223b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1 /* Random objects */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
3 /* ------------------------------------------------------------------
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
4 The code in this module was based on a download from:
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
5 http://www.math.keio.ac.jp/~matumoto/MT2002/emt19937ar.html
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 It was modified in 2002 by Raymond Hettinger as follows:
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
8
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
9 * the principal computational lines untouched except for tabbing.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
11 * renamed genrand_res53() to random_random() and wrapped
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
12 in python calling/return code.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
13
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
14 * genrand_int32() and the helper functions, init_genrand()
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
15 and init_by_array(), were declared static, wrapped in
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
16 Python calling/return code. also, their global data
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
17 references were replaced with structure references.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
18
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
19 * unused functions from the original were deleted.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
20 new, original C python code was added to implement the
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
21 Random() interface.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
22
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
23 The following are the verbatim comments from the original code:
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
24
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
25 A C-program for MT19937, with initialization improved 2002/1/26.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
26 Coded by Takuji Nishimura and Makoto Matsumoto.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
27
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
28 Before using, initialize the state by using init_genrand(seed)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
29 or init_by_array(init_key, key_length).
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
30
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
31 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
32 All rights reserved.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
33
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
34 Redistribution and use in source and binary forms, with or without
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
35 modification, are permitted provided that the following conditions
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
36 are met:
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
37
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
38 1. Redistributions of source code must retain the above copyright
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
39 notice, this list of conditions and the following disclaimer.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
40
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
41 2. Redistributions in binary form must reproduce the above copyright
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
42 notice, this list of conditions and the following disclaimer in the
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
43 documentation and/or other materials provided with the distribution.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
44
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
45 3. The names of its contributors may not be used to endorse or promote
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
46 products derived from this software without specific prior written
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
47 permission.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
48
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
49 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
50 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
51 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
52 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
53 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
54 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
55 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
56 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
57 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
58 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
59 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
60
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
61
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
62 Any feedback is very welcome.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
63 http://www.math.keio.ac.jp/matumoto/emt.html
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
64 email: matumoto@math.keio.ac.jp
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
65 */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
66
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
67 /* ---------------------------------------------------------------*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
68
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
69 #include "Python.h"
28637
acfa0ff8c5a4 Minor source formatting fixes.
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
70 #include <time.h> /* for seeding to current time */
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
71
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
72 /* Period parameters -- These are all magic. Don't change. */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73 #define N 624
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
74 #define M 397
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
75 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
76 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
77 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
78
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
79 typedef struct {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
80 PyObject_HEAD
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
81 unsigned long state[N];
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
82 int index;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
83 } RandomObject;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
84
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 static PyTypeObject Random_Type;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
86
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44122
diff changeset
87 #define RandomObject_Check(v) (Py_TYPE(v) == &Random_Type)
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
88
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
89
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
90 /* Random methods */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93 /* generates a random number on [0,0xffffffff]-interval */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 static unsigned long
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
95 genrand_int32(RandomObject *self)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
96 {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97 unsigned long y;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
98 static unsigned long mag01[2]={0x0UL, MATRIX_A};
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99 /* mag01[x] = x * MATRIX_A for x=0,1 */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100 unsigned long *mt;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
101
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
102 mt = self->state;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
103 if (self->index >= N) { /* generate N words at one time */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
104 int kk;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
105
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
106 for (kk=0;kk<N-M;kk++) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
107 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
108 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
109 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
110 for (;kk<N-1;kk++) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
111 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
112 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
28637
acfa0ff8c5a4 Minor source formatting fixes.
Raymond Hettinger <python@rcn.com>
parents: 27104
diff changeset
113 }
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
114 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
115 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
116
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
117 self->index = 0;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
118 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
119
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
120 y = mt[self->index++];
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
121 y ^= (y >> 11);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
122 y ^= (y << 7) & 0x9d2c5680UL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
123 y ^= (y << 15) & 0xefc60000UL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
124 y ^= (y >> 18);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
125 return y;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
126 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
127
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
128 /* random_random is the function named genrand_res53 in the original code;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
129 * generates a random number on [0,1) with 53-bit resolution; note that
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
130 * 9007199254740992 == 2**53; I assume they're spelling "/2**53" as
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
131 * multiply-by-reciprocal in the (likely vain) hope that the compiler will
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
132 * optimize the division away at compile-time. 67108864 is 2**26. In
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
133 * effect, a contains 27 random bits shifted left 26, and b fills in the
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
134 * lower 26 bits of the 53-bit numerator.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
135 * The orginal code credited Isaku Wada for this algorithm, 2002/01/09.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
136 */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
137 static PyObject *
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
138 random_random(RandomObject *self)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
139 {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
140 unsigned long a=genrand_int32(self)>>5, b=genrand_int32(self)>>6;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
141 return PyFloat_FromDouble((a*67108864.0+b)*(1.0/9007199254740992.0));
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
142 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
143
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
144 /* initializes mt[N] with a seed */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
145 static void
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
146 init_genrand(RandomObject *self, unsigned long s)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
147 {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
148 int mti;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
149 unsigned long *mt;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
150
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
151 mt = self->state;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
152 mt[0]= s & 0xffffffffUL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
153 for (mti=1; mti<N; mti++) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
154 mt[mti] =
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
155 (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
156 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
157 /* In the previous versions, MSBs of the seed affect */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
158 /* only MSBs of the array mt[]. */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
159 /* 2002/01/09 modified by Makoto Matsumoto */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
160 mt[mti] &= 0xffffffffUL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
161 /* for >32 bit machines */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
162 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
163 self->index = mti;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
164 return;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
165 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
166
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
167 /* initialize by an array with array-length */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
168 /* init_key is the array for initializing keys */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
169 /* key_length is its length */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
170 static PyObject *
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
171 init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_length)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
172 {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
173 unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
174 unsigned long *mt;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
175
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
176 mt = self->state;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
177 init_genrand(self, 19650218UL);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
178 i=1; j=0;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
179 k = (N>key_length ? N : key_length);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
180 for (; k; k--) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
181 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
182 + init_key[j] + j; /* non linear */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
183 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
184 i++; j++;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
185 if (i>=N) { mt[0] = mt[N-1]; i=1; }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
186 if (j>=key_length) j=0;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
187 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
188 for (k=N-1; k; k--) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
189 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
190 - i; /* non linear */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
191 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
192 i++;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
193 if (i>=N) { mt[0] = mt[N-1]; i=1; }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
194 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
195
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
196 mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
197 Py_INCREF(Py_None);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
198 return Py_None;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
199 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
200
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
201 /*
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
202 * The rest is Python-specific code, neither part of, nor derived from, the
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
203 * Twister download.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
204 */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
205
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
206 static PyObject *
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
207 random_seed(RandomObject *self, PyObject *args)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
208 {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
209 PyObject *result = NULL; /* guilty until proved innocent */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
210 PyObject *masklower = NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
211 PyObject *thirtytwo = NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
212 PyObject *n = NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
213 unsigned long *key = NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
214 unsigned long keymax; /* # of allocated slots in key */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
215 unsigned long keyused; /* # of used slots in key */
28734
5c886e03b43e PyObject_IsTrue() can return an error condition.
Raymond Hettinger <python@rcn.com>
parents: 28637
diff changeset
216 int err;
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
217
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
218 PyObject *arg = NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
219
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
220 if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg))
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
221 return NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
222
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
223 if (arg == NULL || arg == Py_None) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
224 time_t now;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
225
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
226 time(&now);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
227 init_genrand(self, (unsigned long)now);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
228 Py_INCREF(Py_None);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
229 return Py_None;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
230 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
231 /* If the arg is an int or long, use its absolute value; else use
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
232 * the absolute value of its hash code.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
233 */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
234 if (PyInt_Check(arg) || PyLong_Check(arg))
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
235 n = PyNumber_Absolute(arg);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
236 else {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
237 long hash = PyObject_Hash(arg);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
238 if (hash == -1)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
239 goto Done;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
240 n = PyLong_FromUnsignedLong((unsigned long)hash);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
241 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
242 if (n == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
243 goto Done;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
244
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
245 /* Now split n into 32-bit chunks, from the right. Each piece is
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
246 * stored into key, which has a capacity of keymax chunks, of which
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
247 * keyused are filled. Alas, the repeated shifting makes this a
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
248 * quadratic-time algorithm; we'd really like to use
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
249 * _PyLong_AsByteArray here, but then we'd have to break into the
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
250 * long representation to figure out how big an array was needed
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
251 * in advance.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
252 */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
253 keymax = 8; /* arbitrary; grows later if needed */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
254 keyused = 0;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
255 key = (unsigned long *)PyMem_Malloc(keymax * sizeof(*key));
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
256 if (key == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
257 goto Done;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
258
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
259 masklower = PyLong_FromUnsignedLong(0xffffffffU);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
260 if (masklower == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
261 goto Done;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
262 thirtytwo = PyInt_FromLong(32L);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
263 if (thirtytwo == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
264 goto Done;
28734
5c886e03b43e PyObject_IsTrue() can return an error condition.
Raymond Hettinger <python@rcn.com>
parents: 28637
diff changeset
265 while ((err=PyObject_IsTrue(n))) {
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
266 PyObject *newn;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
267 PyObject *pychunk;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
268 unsigned long chunk;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
269
28734
5c886e03b43e PyObject_IsTrue() can return an error condition.
Raymond Hettinger <python@rcn.com>
parents: 28637
diff changeset
270 if (err == -1)
5c886e03b43e PyObject_IsTrue() can return an error condition.
Raymond Hettinger <python@rcn.com>
parents: 28637
diff changeset
271 goto Done;
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
272 pychunk = PyNumber_And(n, masklower);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
273 if (pychunk == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
274 goto Done;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
275 chunk = PyLong_AsUnsignedLong(pychunk);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
276 Py_DECREF(pychunk);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
277 if (chunk == (unsigned long)-1 && PyErr_Occurred())
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
278 goto Done;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
279 newn = PyNumber_Rshift(n, thirtytwo);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
280 if (newn == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
281 goto Done;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
282 Py_DECREF(n);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
283 n = newn;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
284 if (keyused >= keymax) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
285 unsigned long bigger = keymax << 1;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
286 if ((bigger >> 1) != keymax) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
287 PyErr_NoMemory();
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
288 goto Done;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
289 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
290 key = (unsigned long *)PyMem_Realloc(key,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
291 bigger * sizeof(*key));
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
292 if (key == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
293 goto Done;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
294 keymax = bigger;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
295 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
296 assert(keyused < keymax);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
297 key[keyused++] = chunk;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
298 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
299
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
300 if (keyused == 0)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
301 key[keyused++] = 0UL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
302 result = init_by_array(self, key, keyused);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
303 Done:
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
304 Py_XDECREF(masklower);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
305 Py_XDECREF(thirtytwo);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
306 Py_XDECREF(n);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
307 PyMem_Free(key);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
308 return result;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
309 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
310
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
311 static PyObject *
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
312 random_getstate(RandomObject *self)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
313 {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
314 PyObject *state;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
315 PyObject *element;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
316 int i;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
317
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
318 state = PyTuple_New(N+1);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
319 if (state == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
320 return NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
321 for (i=0; i<N ; i++) {
44122
97210d863d12 Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
322 element = PyLong_FromUnsignedLong(self->state[i]);
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
323 if (element == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
324 goto Fail;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
325 PyTuple_SET_ITEM(state, i, element);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
326 }
44122
97210d863d12 Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
327 element = PyLong_FromLong((long)(self->index));
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
328 if (element == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
329 goto Fail;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
330 PyTuple_SET_ITEM(state, i, element);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
331 return state;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
332
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
333 Fail:
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
334 Py_DECREF(state);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
335 return NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
336 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
337
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
338 static PyObject *
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
339 random_setstate(RandomObject *self, PyObject *state)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
340 {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
341 int i;
44122
97210d863d12 Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
342 unsigned long element;
97210d863d12 Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
343 long index;
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
344
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
345 if (!PyTuple_Check(state)) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
346 PyErr_SetString(PyExc_TypeError,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
347 "state vector must be a tuple");
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
348 return NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
349 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
350 if (PyTuple_Size(state) != N+1) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
351 PyErr_SetString(PyExc_ValueError,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
352 "state vector is the wrong size");
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
353 return NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
354 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
355
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
356 for (i=0; i<N ; i++) {
44122
97210d863d12 Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
357 element = PyLong_AsUnsignedLong(PyTuple_GET_ITEM(state, i));
53799
6d64cd69f384 Merged revisions 72344 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 44350
diff changeset
358 if (element == (unsigned long)-1 && PyErr_Occurred())
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
359 return NULL;
44122
97210d863d12 Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
360 self->state[i] = element & 0xffffffffUL; /* Make sure we get sane state */
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
361 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
362
44122
97210d863d12 Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
363 index = PyLong_AsLong(PyTuple_GET_ITEM(state, i));
97210d863d12 Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
364 if (index == -1 && PyErr_Occurred())
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
365 return NULL;
44122
97210d863d12 Issue #1727780: Support loading pickles of random.Random objects created
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
366 self->index = (int)index;
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
367
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
368 Py_INCREF(Py_None);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
369 return Py_None;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
370 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
371
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
372 /*
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
373 Jumpahead should be a fast way advance the generator n-steps ahead, but
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
374 lacking a formula for that, the next best is to use n and the existing
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
375 state to create a new state far away from the original.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
376
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
377 The generator uses constant spaced additive feedback, so shuffling the
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
378 state elements ought to produce a state which would not be encountered
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
379 (in the near term) by calls to random(). Shuffling is normally
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
380 implemented by swapping the ith element with another element ranging
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
381 from 0 to i inclusive. That allows the element to have the possibility
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
382 of not being moved. Since the goal is to produce a new, different
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
383 state, the swap element is ranged from 0 to i-1 inclusive. This assures
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
384 that each element gets moved at least once.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
385
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
386 To make sure that consecutive calls to jumpahead(n) produce different
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
387 states (even in the rare case of involutory shuffles), i+1 is added to
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
388 each element at position i. Successive calls are then guaranteed to
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
389 have changing (growing) values as well as shuffled positions.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
390
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
391 Finally, the self->index value is set to N so that the generator itself
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
392 kicks in on the next call to random(). This assures that all results
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
393 have been through the generator and do not just reflect alterations to
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
394 the underlying state.
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
395 */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
396
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
397 static PyObject *
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
398 random_jumpahead(RandomObject *self, PyObject *n)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
399 {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
400 long i, j;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
401 PyObject *iobj;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
402 PyObject *remobj;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
403 unsigned long *mt, tmp;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
404
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
405 if (!PyInt_Check(n) && !PyLong_Check(n)) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
406 PyErr_Format(PyExc_TypeError, "jumpahead requires an "
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
407 "integer, not '%s'",
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 44122
diff changeset
408 Py_TYPE(n)->tp_name);
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
409 return NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
410 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
411
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
412 mt = self->state;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
413 for (i = N-1; i > 1; i--) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
414 iobj = PyInt_FromLong(i);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
415 if (iobj == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
416 return NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
417 remobj = PyNumber_Remainder(n, iobj);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
418 Py_DECREF(iobj);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
419 if (remobj == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
420 return NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
421 j = PyInt_AsLong(remobj);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
422 Py_DECREF(remobj);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
423 if (j == -1L && PyErr_Occurred())
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
424 return NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
425 tmp = mt[i];
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
426 mt[i] = mt[j];
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
427 mt[j] = tmp;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
428 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
429
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
430 for (i = 0; i < N; i++)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
431 mt[i] += i+1;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
432
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
433 self->index = N;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
434 Py_INCREF(Py_None);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
435 return Py_None;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
436 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
437
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
438 static PyObject *
30649
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
439 random_getrandbits(RandomObject *self, PyObject *args)
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
440 {
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
441 int k, i, bytes;
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
442 unsigned long r;
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
443 unsigned char *bytearray;
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
444 PyObject *result;
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
445
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
446 if (!PyArg_ParseTuple(args, "i:getrandbits", &k))
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
447 return NULL;
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
448
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
449 if (k <= 0) {
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
450 PyErr_SetString(PyExc_ValueError,
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
451 "number of bits must be greater than zero");
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
452 return NULL;
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
453 }
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
454
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
455 bytes = ((k - 1) / 32 + 1) * 4;
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
456 bytearray = (unsigned char *)PyMem_Malloc(bytes);
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
457 if (bytearray == NULL) {
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
458 PyErr_NoMemory();
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
459 return NULL;
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
460 }
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
461
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
462 /* Fill-out whole words, byte-by-byte to avoid endianness issues */
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
463 for (i=0 ; i<bytes ; i+=4, k-=32) {
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
464 r = genrand_int32(self);
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
465 if (k < 32)
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
466 r >>= (32 - k);
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
467 bytearray[i+0] = (unsigned char)r;
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
468 bytearray[i+1] = (unsigned char)(r >> 8);
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
469 bytearray[i+2] = (unsigned char)(r >> 16);
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
470 bytearray[i+3] = (unsigned char)(r >> 24);
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
471 }
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
472
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
473 /* little endian order to match bytearray assignment order */
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
474 result = _PyLong_FromByteArray(bytearray, bytes, 1, 0);
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
475 PyMem_Free(bytearray);
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
476 return result;
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
477 }
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
478
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
479 static PyObject *
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
480 random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
481 {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
482 RandomObject *self;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
483 PyObject *tmp;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
484
40682
77e0f4718a7c Bug #1486663: don't reject keyword arguments for subclasses of builtin
Georg Brandl <georg@python.org>
parents: 36324
diff changeset
485 if (type == &Random_Type && !_PyArg_NoKeywords("Random()", kwds))
35450
97c97194e64b Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents: 30649
diff changeset
486 return NULL;
97c97194e64b Disallow keyword arguments for type constructors that don't use them
Georg Brandl <georg@python.org>
parents: 30649
diff changeset
487
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
488 self = (RandomObject *)type->tp_alloc(type, 0);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
489 if (self == NULL)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
490 return NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
491 tmp = random_seed(self, args);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
492 if (tmp == NULL) {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
493 Py_DECREF(self);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
494 return NULL;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
495 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
496 Py_DECREF(tmp);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
497 return (PyObject *)self;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
498 }
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
499
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
500 static PyMethodDef random_methods[] = {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
501 {"random", (PyCFunction)random_random, METH_NOARGS,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
502 PyDoc_STR("random() -> x in the interval [0, 1).")},
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
503 {"seed", (PyCFunction)random_seed, METH_VARARGS,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
504 PyDoc_STR("seed([n]) -> None. Defaults to current time.")},
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
505 {"getstate", (PyCFunction)random_getstate, METH_NOARGS,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
506 PyDoc_STR("getstate() -> tuple containing the current state.")},
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
507 {"setstate", (PyCFunction)random_setstate, METH_O,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
508 PyDoc_STR("setstate(state) -> None. Restores generator state.")},
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
509 {"jumpahead", (PyCFunction)random_jumpahead, METH_O,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
510 PyDoc_STR("jumpahead(int) -> None. Create new state from "
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
511 "existing state and integer.")},
30649
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
512 {"getrandbits", (PyCFunction)random_getrandbits, METH_VARARGS,
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
513 PyDoc_STR("getrandbits(k) -> x. Generates a long int with "
8841d34a2c37 SF bug #812202: randint is always even
Raymond Hettinger <python@rcn.com>
parents: 29242
diff changeset
514 "k random bits.")},
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
515 {NULL, NULL} /* sentinel */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
516 };
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
517
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
518 PyDoc_STRVAR(random_doc,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
519 "Random() -> create a random number generator with its own internal state.");
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
520
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
521 static PyTypeObject Random_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 40682
diff changeset
522 PyVarObject_HEAD_INIT(NULL, 0)
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
523 "_random.Random", /*tp_name*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
524 sizeof(RandomObject), /*tp_basicsize*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
525 0, /*tp_itemsize*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
526 /* methods */
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
527 0, /*tp_dealloc*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
528 0, /*tp_print*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
529 0, /*tp_getattr*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
530 0, /*tp_setattr*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
531 0, /*tp_compare*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
532 0, /*tp_repr*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
533 0, /*tp_as_number*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
534 0, /*tp_as_sequence*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
535 0, /*tp_as_mapping*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
536 0, /*tp_hash*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
537 0, /*tp_call*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
538 0, /*tp_str*/
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 26990
diff changeset
539 PyObject_GenericGetAttr, /*tp_getattro*/
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
540 0, /*tp_setattro*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
541 0, /*tp_as_buffer*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
542 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
543 random_doc, /*tp_doc*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
544 0, /*tp_traverse*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
545 0, /*tp_clear*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
546 0, /*tp_richcompare*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
547 0, /*tp_weaklistoffset*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
548 0, /*tp_iter*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
549 0, /*tp_iternext*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
550 random_methods, /*tp_methods*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
551 0, /*tp_members*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
552 0, /*tp_getset*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
553 0, /*tp_base*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
554 0, /*tp_dict*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
555 0, /*tp_descr_get*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
556 0, /*tp_descr_set*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
557 0, /*tp_dictoffset*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
558 0, /*tp_init*/
29242
d72ad07c739e PyType_GenericAlloc is inherited from object.
Raymond Hettinger <python@rcn.com>
parents: 28734
diff changeset
559 0, /*tp_alloc*/
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
560 random_new, /*tp_new*/
27104
2443c557cbc3 Patch #661760: Cygwin auto-import module patch
Jason Tishler <jason@tishler.net>
parents: 26990
diff changeset
561 _PyObject_Del, /*tp_free*/
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
562 0, /*tp_is_gc*/
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
563 };
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
564
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
565 PyDoc_STRVAR(module_doc,
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
566 "Module implements the Mersenne Twister random number generator.");
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
567
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
568 PyMODINIT_FUNC
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
569 init_random(void)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
570 {
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
571 PyObject *m;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
572
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
573 if (PyType_Ready(&Random_Type) < 0)
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
574 return;
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
575 m = Py_InitModule3("_random", NULL, module_doc);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35450
diff changeset
576 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35450
diff changeset
577 return;
26829
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
578 Py_INCREF(&Random_Type);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
579 PyModule_AddObject(m, "Random", (PyObject *)&Random_Type);
f64c96922708 SF patch 658251: Install a C implementation of the Mersenne Twister as the
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
580 }