Debian Codegraph @

ext/mt-random/mt-random.h

raw
1 /*
2 * mt-random.h - implements MT19937 random number generation algorithm
3 * This code is based on Makoto Matsumoto & Takuji Nishimura's mt18837ar.c
4 * See mt-random.c for details.
5 *
6 * The original copyright notice follows.
7 */
8 /*
9 A C-program for MT19937, with initialization improved 2002/1/26.
10 Coded by Takuji Nishimura and Makoto Matsumoto.
11
12 Before using, initialize the state by using init_genrand(seed)
13 or init_by_array(init_key, key_length).
14
15 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
16 All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions
20 are met:
21
22 1. Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
24
25 2. Redistributions in binary form must reproduce the above copyright
26 notice, this list of conditions and the following disclaimer in the
27 documentation and/or other materials provided with the distribution.
28
29 3. The names of its contributors may not be used to endorse or promote
30 products derived from this software without specific prior written
31 permission.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45
46 Any feedback is very welcome.
47 http://www.math.keio.ac.jp/matumoto/emt.html
48 email: [email protected]
49 */
50
51 #include <gauche.h>
52 #include <gauche/priv/configP.h>
53 #include <gauche/extend.h>
54
55 #if defined(EXTMTRANDOM_EXPORTS)
56 #define LIBGAUCHE_EXT_BODY
57 #endif
58 #include <gauche/extern.h> /* redefine SCM_EXTERN */
59
60 #define N 624
61
62 typedef struct ScmMersenneTwisterRec {
63 SCM_HEADER;
64 unsigned long mt[N]; /* the array for the state vector */
65 int mti; /* index into the state vector.
66 new integer random number is computed from
67 mt[mti], incrementing mti; once it exceeds N,
68 the entire state vector is recomputed. */
69 u_int flags;
70 ScmObj seed; /* last seed value used to initialize this instance.
71 useful to reproduce the same random sequence. */
72 ScmInternalMutex lock;
73 } ScmMersenneTwister;
74
75 /* flags */
76 enum {
77 /* Do not use mutex. Faster if the user knows the generator
78 is only used in one thread, or is a part of mutexed structure.
79
80 A simple micro-benchmark shows skipping lock makes mt-random-real
81 about 20% faster. Most use cases probably don't need to care; only
82 for performance sensitive code. */
83 SCM_MERSENNE_TWISTER_PRIVATE = (1L<<0)
84 };
85
86 SCM_CLASS_DECL(Scm_MersenneTwisterClass);
87 #define SCM_MERSENNE_TWISTER(obj) ((ScmMersenneTwister*)obj)
88 #define SCM_MERSENNE_TWISTER_P(obj) SCM_XTYPEP(obj, &Scm_MersenneTwisterClass)
89
90 #define SCM_MERSENNE_TWISTER_NEED_LOCK(m) \
91 (!((m)->flags & SCM_MERSENNE_TWISTER_PRIVATE))
92
93 extern ScmObj Scm_MakeMT(ScmObj seed, u_int flags);
94 extern void Scm_MTSetSeed(ScmMersenneTwister *mt, ScmObj seed);
95 extern ScmObj Scm_MTGetState(ScmMersenneTwister *mt);
96 extern void Scm_MTSetState(ScmMersenneTwister *mt, ScmU32Vector *v);
97
98 extern u_long Scm_MTGenrandU32(ScmMersenneTwister *);
99 extern float Scm_MTGenrandF32(ScmMersenneTwister *, int);
100 extern double Scm_MTGenrandF64(ScmMersenneTwister *, int);
101 extern ScmObj Scm_MTGenrandInt(ScmMersenneTwister *mt, ScmObj n);
102
103 extern ScmObj Scm_MTFillUvector(ScmMersenneTwister *, ScmObj);
104
105 extern void Scm_Init_mt_random(void);