Select the types of activity you want to include in your feed.
[READ-ONLY] Mirror of https://github.com/shuuji3/python-extension-twister. Experimental Python C extension to generate random numbers by using Mersenne twister
···11+/*
22+ A C-program for MT19937, with initialization improved 2002/1/26.
33+ Coded by Takuji Nishimura and Makoto Matsumoto.
44+55+ Before using, initialize the state by using init_genrand(seed)
66+ or init_by_array(init_key, key_length).
77+88+ Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
99+ All rights reserved.
1010+ Copyright (C) 2005, Mutsuo Saito
1111+ All rights reserved.
1212+1313+ Redistribution and use in source and binary forms, with or without
1414+ modification, are permitted provided that the following conditions
1515+ are met:
1616+1717+ 1. Redistributions of source code must retain the above copyright
1818+ notice, this list of conditions and the following disclaimer.
1919+2020+ 2. Redistributions in binary form must reproduce the above copyright
2121+ notice, this list of conditions and the following disclaimer in the
2222+ documentation and/or other materials provided with the distribution.
2323+2424+ 3. The names of its contributors may not be used to endorse or promote
2525+ products derived from this software without specific prior written
2626+ permission.
2727+2828+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2929+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3030+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3131+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3232+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3333+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3434+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3535+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3636+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3737+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3838+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3939+4040+4141+ Any feedback is very welcome.
4242+ http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
4343+ email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
4444+*/
4545+4646+/* initializes mt[N] with a seed */
4747+void init_genrand(unsigned long s);
4848+4949+/* initialize by an array with array-length */
5050+/* init_key is the array for initializing keys */
5151+/* key_length is its length */
5252+/* slight change for C++, 2004/2/26 */
5353+void init_by_array(unsigned long init_key[], int key_length);
5454+5555+/* generates a random number on [0,0xffffffff]-interval */
5656+unsigned long genrand_int32(void);
5757+5858+/* generates a random number on [0,0x7fffffff]-interval */
5959+long genrand_int31(void);
6060+6161+/* These real versions are due to Isaku Wada, 2002/01/09 added */
6262+/* generates a random number on [0,1]-real-interval */
6363+double genrand_real1(void);
6464+6565+/* generates a random number on [0,1)-real-interval */
6666+double genrand_real2(void);
6767+6868+/* generates a random number on (0,1)-real-interval */
6969+double genrand_real3(void);
7070+7171+/* generates a random number on [0,1) with 53-bit resolution*/
7272+double genrand_res53(void);
···11+/*
22+ A C-program for MT19937, with initialization improved 2002/1/26.
33+ Coded by Takuji Nishimura and Makoto Matsumoto.
44+55+ Before using, initialize the state by using init_genrand(seed)
66+ or init_by_array(init_key, key_length).
77+88+ Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
99+ All rights reserved.
1010+ Copyright (C) 2005, Mutsuo Saito,
1111+ All rights reserved.
1212+1313+ Redistribution and use in source and binary forms, with or without
1414+ modification, are permitted provided that the following conditions
1515+ are met:
1616+1717+ 1. Redistributions of source code must retain the above copyright
1818+ notice, this list of conditions and the following disclaimer.
1919+2020+ 2. Redistributions in binary form must reproduce the above copyright
2121+ notice, this list of conditions and the following disclaimer in the
2222+ documentation and/or other materials provided with the distribution.
2323+2424+ 3. The names of its contributors may not be used to endorse or promote
2525+ products derived from this software without specific prior written
2626+ permission.
2727+2828+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2929+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3030+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3131+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3232+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3333+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3434+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3535+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3636+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3737+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3838+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3939+4040+4141+ Any feedback is very welcome.
4242+ http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
4343+ email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
4444+*/
4545+4646+#include <stdio.h>
4747+#include "mt19937ar.h"
4848+4949+int main(void)
5050+{
5151+ int i;
5252+ unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4;
5353+ init_by_array(init, length);
5454+ printf("1000 outputs of genrand_int32()\n");
5555+ for (i=0; i<1000; i++) {
5656+ printf("%10lu ", genrand_int32());
5757+ if (i%5==4) printf("\n");
5858+ }
5959+ printf("\n1000 outputs of genrand_real2()\n");
6060+ for (i=0; i<1000; i++) {
6161+ printf("%10.8f ", genrand_real2());
6262+ if (i%5==4) printf("\n");
6363+ }
6464+ return 0;
6565+}
···11+This is a Mersenne Twister pseudorandom number generator
22+with period 2^19937-1 with improved initialization scheme,
33+modified on 2002/1/26 by Takuji Nishimura and Makoto Matsumoto.
44+modified on 2005/4/26 by Mutsuo Saito
55+66+Contents of this tar ball:
77+readme-mt.txt this file
88+mt19937ar.c the C source (ar: initialize by ARray)
99+mt19937ar.h the C header file for mt19937ar
1010+mtTest.c the C test main program of mt19937ar.c
1111+mt19937ar.out Test outputs of six types generators. 1000 for each
1212+1313+1. Initialization
1414+ The initialization scheme for the previous versions of MT
1515+(e.g. 1999/10/28 version or earlier) has a tiny problem, that
1616+the most significant bits of the seed is not well reflected
1717+to the state vector of MT.
1818+1919+This version (2002/1/26) has two initialization schemes:
2020+init_genrand(seed) and init_by_array(init_key, key_length).
2121+2222+init_genrand(seed) initializes the state vector by using
2323+one unsigned 32-bit integer "seed", which may be zero.
2424+2525+init_by_array(init_key, key_length) initializes the state vector
2626+by using an array init_key[] of unsigned 32-bit integers
2727+of length key_kength. If key_length is smaller than 624,
2828+then each array of 32-bit integers gives distinct initial
2929+state vector. This is useful if you want a larger seed space
3030+than 32-bit word.
3131+3232+2. Generation
3333+After initialization, the following type of pseudorandom numbers
3434+are available.
3535+3636+genrand_int32() generates unsigned 32-bit integers.
3737+genrand_int31() generates unsigned 31-bit integers.
3838+genrand_real1() generates uniform real in [0,1] (32-bit resolution).
3939+genrand_real2() generates uniform real in [0,1) (32-bit resolution).
4040+genrand_real3() generates uniform real in (0,1) (32-bit resolution).
4141+genrand_res53() generates uniform real in [0,1) with 53-bit resolution.
4242+4343+Note: the last five functions call the first one.
4444+if you need more speed for these five functions, you may
4545+suppress the function call by copying genrand_int32() and
4646+replacing the last return(), following to these five functions.
4747+4848+3. main()
4949+main() is an example to initialize with an array of length 4,
5050+then 1000 outputs of unsigned 32-bit integers,
5151+then 1000 outputs of real [0,1) numbers.
5252+5353+4. The outputs
5454+The output of the mt19937ar.c is in the file mt19937ar.out.
5555+If you revise or translate the code, check the output
5656+by using this file.
5757+5858+5. Cryptography
5959+This generator is not cryptoraphically secure.
6060+You need to use a one-way (or hash) function to obtain
6161+a secure random sequence.
6262+6363+6. Correspondence
6464+See:
6565+URL http://www.math.keio.ac.jp/matumoto/emt.html
6666+email matumoto@math.keio.ac.jp, nisimura@sci.kj.yamagata-u.ac.jp
6767+6868+7. Reference
6969+M. Matsumoto and T. Nishimura,
7070+"Mersenne Twister: A 623-Dimensionally Equidistributed Uniform
7171+Pseudo-Random Number Generator",
7272+ACM Transactions on Modeling and Computer Simulation,
7373+Vol. 8, No. 1, January 1998, pp 3--30.
7474+7575+-------
7676+Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
7777+All rights reserved.
7878+Copyright (C) 2005, Mutsuo Saito
7979+All rights reserved.
···11-/*
11+/*
22 A C-program for MT19937, with initialization improved 2002/1/26.
33 Coded by Takuji Nishimura and Makoto Matsumoto.
4455- Before using, initialize the state by using init_genrand(seed)
55+ Before using, initialize the state by using init_genrand(seed)
66 or init_by_array(init_key, key_length).
7788 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
99- All rights reserved.
99+ All rights reserved.
1010+ Copyright (C) 2005, Mutsuo Saito,
1111+ All rights reserved.
10121113 Redistribution and use in source and binary forms, with or without
1214 modification, are permitted provided that the following conditions
···1921 notice, this list of conditions and the following disclaimer in the
2022 documentation and/or other materials provided with the distribution.
21232222- 3. The names of its contributors may not be used to endorse or promote
2323- products derived from this software without specific prior written
2424+ 3. The names of its contributors may not be used to endorse or promote
2525+ products derived from this software without specific prior written
2426 permission.
25272628 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
···4244*/
43454446#include <stdio.h>
4747+#include "mt19937ar.h"
45484646-/* Period parameters */
4949+/* Period parameters */
4750#define N 624
4851#define M 397
4952#define MATRIX_A 0x9908b0dfUL /* constant vector a */
···5861{
5962 mt[0]= s & 0xffffffffUL;
6063 for (mti=1; mti<N; mti++) {
6161- mt[mti] =
6262- (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
6464+ mt[mti] =
6565+ (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
6366 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
6467 /* In the previous versions, MSBs of the seed affect */
6568 /* only MSBs of the array mt[]. */
···7376/* init_key is the array for initializing keys */
7477/* key_length is its length */
7578/* slight change for C++, 2004/2/26 */
7676-void init_by_array(const unsigned long init_key[], int key_length)
7979+void init_by_array(unsigned long init_key[], int key_length)
7780{
7881 int i, j, k;
7982 init_genrand(19650218UL);
···8184 k = (N>key_length ? N : key_length);
8285 for (; k; k--) {
8386 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
8484- + init_key[j] + j; /* non linear */
8787+ + init_key[j] + j; /* non linear */
8588 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
8689 i++; j++;
8790 if (i>=N) { mt[0] = mt[N-1]; i=1; }
···8992 }
9093 for (k=N-1; k; k--) {
9194 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
9292- - i; /* non linear */
9595+ - i; /* non linear */
9396 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
9497 i++;
9598 if (i>=N) { mt[0] = mt[N-1]; i=1; }
9699 }
971009898- mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
101101+ mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
99102}
100103101104/* generates a random number on [0,0xffffffff]-interval */
···124127125128 mti = 0;
126129 }
127127-130130+128131 y = mt[mti++];
129132130133 /* Tempering */
···145148/* generates a random number on [0,1]-real-interval */
146149double genrand_real1(void)
147150{
148148- return genrand_int32()*(1.0/4294967295.0);
149149- /* divided by 2^32-1 */
151151+ return genrand_int32()*(1.0/4294967295.0);
152152+ /* divided by 2^32-1 */
150153}
151154152155/* generates a random number on [0,1)-real-interval */
153156double genrand_real2(void)
154157{
155155- return genrand_int32()*(1.0/4294967296.0);
158158+ return genrand_int32()*(1.0/4294967296.0);
156159 /* divided by 2^32 */
157160}
158161159162/* generates a random number on (0,1)-real-interval */
160163double genrand_real3(void)
161164{
162162- return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
165165+ return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
163166 /* divided by 2^32 */
164167}
165168166169/* generates a random number on [0,1) with 53-bit resolution*/
167167-double genrand_res53(void)
168168-{
169169- unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
170170- return(a*67108864.0+b)*(1.0/9007199254740992.0);
171171-}
170170+double genrand_res53(void)
171171+{
172172+ unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
173173+ return(a*67108864.0+b)*(1.0/9007199254740992.0);
174174+}
172175/* These real versions are due to Isaku Wada, 2002/01/09 added */
-13
twister.h
···11-#ifndef PYTHON_EXTENSION_TWISTER_H
22-#define PYTHON_EXTENSION_TWISTER_H
33-44-void init_genrand(unsigned long s);
55-void init_by_array(const unsigned long init_key[], int key_length);
66-unsigned long genrand_int32(void);
77-long genrand_int31(void);
88-double genrand_real1(void);
99-double genrand_real2(void);
1010-double genrand_real3(void);
1111-double genrand_res53(void);
1212-1313-#endif //PYTHON_EXTENSION_TWISTER_H
-69
twistermodule.c
···11-#include <Python.h>
22-33-#include <time.h>
44-#include "twister.h"
55-66-// 整数の乱数を20個 print する関数
77-void print_rand_int(int num) {
88- printf("# print %d random int\n", num);
99- for (int i = 0; i < num; i++) {
1010- printf("%10lu ", genrand_int32());
1111- if (i % 5 == 4) printf("\n");
1212- }
1313-}
1414-1515-// 0 <= r < 1 の乱数を20個 print する関数
1616-void print_rand(int num) {
1717- printf("# print %d random number\n", num);
1818- for (int i = 0; i < num; i++) {
1919- printf("%10.8f ", genrand_real2());
2020- if (i % 5 == 4) printf("\n");
2121- }
2222-}
2323-2424-static PyObject *
2525-twister_print_rand_int(PyObject *self, PyObject *args) {
2626- int num = 1;
2727- if (!PyArg_ParseTuple(args, "|l", &num)) {
2828- return NULL;
2929- }
3030- print_rand_int(num);
3131- Py_RETURN_NONE;
3232-}
3333-3434-static PyObject *
3535-twister_print_rand(PyObject *self, PyObject *args) {
3636- int num = 1;
3737- if (!PyArg_ParseTuple(args, "|l", &num)) {
3838- return NULL;
3939- }
4040- print_rand(num);
4141- Py_RETURN_NONE;
4242-}
4343-4444-static PyMethodDef TwisterMethods[] = {
4545- {"print_rand_int", twister_print_rand_int, METH_VARARGS, "Print random int num times."},
4646- {"print_rand", twister_print_rand, METH_VARARGS, "Print random r (0 <= r < 1) num times."},
4747- {NULL, NULL},
4848-};
4949-5050-static struct PyModuleDef twistermodule = {
5151- PyModuleDef_HEAD_INIT,
5252- "twister",
5353- "Generate random numbers by using Mersenne twister.",
5454- -1,
5555- TwisterMethods,
5656-};
5757-5858-PyMODINIT_FUNC
5959-PyInit_twister(void) {
6060- // モジュール読み込み時に現在時刻で乱数を初期化する
6161- init_genrand((unsigned long) time(NULL));
6262-6363- return PyModule_Create(&twistermodule);
6464-}
6565-6666-int main(void) {
6767- print_rand(10);
6868- print_rand_int(20);
6969-}