2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
random.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Fri Dec 24 16:39:21 JST 1993
|
|
|
|
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-25 07:29:39 +04:00
|
|
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2009-02-22 17:23:33 +03:00
|
|
|
/*
|
2002-07-26 10:12:39 +04:00
|
|
|
This is based on trimmed version of MT19937. To get the original version,
|
2007-11-09 01:27:54 +03:00
|
|
|
contact <http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html>.
|
2002-07-26 10:12:39 +04:00
|
|
|
|
|
|
|
The original copyright notice follows.
|
|
|
|
|
|
|
|
A C-program for MT19937, with initialization improved 2002/2/10.
|
|
|
|
Coded by Takuji Nishimura and Makoto Matsumoto.
|
|
|
|
This is a faster version by taking Shawn Cokus's optimization,
|
|
|
|
Matthe Bellew's simplification, Isaku Wada's real version.
|
|
|
|
|
2008-08-17 06:43:20 +04:00
|
|
|
Before using, initialize the state by using init_genrand(mt, seed)
|
|
|
|
or init_by_array(mt, init_key, key_length).
|
2002-07-26 10:12:39 +04:00
|
|
|
|
|
|
|
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
|
2009-02-22 17:23:33 +03:00
|
|
|
All rights reserved.
|
2002-07-26 10:12:39 +04:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
2009-02-22 17:23:33 +03:00
|
|
|
3. The names of its contributors may not be used to endorse or promote
|
|
|
|
products derived from this software without specific prior written
|
2002-07-26 10:12:39 +04:00
|
|
|
permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
|
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
Any feedback is very welcome.
|
|
|
|
http://www.math.keio.ac.jp/matumoto/emt.html
|
|
|
|
email: matumoto@math.keio.ac.jp
|
|
|
|
*/
|
|
|
|
|
2009-07-10 09:44:08 +04:00
|
|
|
#include <limits.h>
|
|
|
|
typedef int int_must_be_32bit_at_least[sizeof(int) * CHAR_BIT < 32 ? -1 : 1];
|
|
|
|
|
2009-02-22 17:23:33 +03:00
|
|
|
/* Period parameters */
|
2002-07-26 10:12:39 +04:00
|
|
|
#define N 624
|
|
|
|
#define M 397
|
2009-07-10 09:44:08 +04:00
|
|
|
#define MATRIX_A 0x9908b0dfU /* constant vector a */
|
|
|
|
#define UMASK 0x80000000U /* most significant w-r bits */
|
|
|
|
#define LMASK 0x7fffffffU /* least significant r bits */
|
2002-07-26 10:12:39 +04:00
|
|
|
#define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
|
2009-07-10 09:44:08 +04:00
|
|
|
#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1U ? MATRIX_A : 0U))
|
|
|
|
|
|
|
|
enum {MT_MAX_STATE = N};
|
2002-07-26 10:12:39 +04:00
|
|
|
|
2008-08-17 06:43:20 +04:00
|
|
|
struct MT {
|
2009-07-10 09:44:08 +04:00
|
|
|
/* assume int is enough to store 32bits */
|
|
|
|
unsigned int state[N]; /* the array for the state vector */
|
|
|
|
unsigned int *next;
|
2008-08-17 06:43:20 +04:00
|
|
|
int left;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define genrand_initialized(mt) ((mt)->next != 0)
|
|
|
|
#define uninit_genrand(mt) ((mt)->next = 0)
|
2002-07-26 10:12:39 +04:00
|
|
|
|
|
|
|
/* initializes state[N] with a seed */
|
|
|
|
static void
|
2009-07-10 09:44:08 +04:00
|
|
|
init_genrand(struct MT *mt, unsigned int s)
|
2002-07-26 10:12:39 +04:00
|
|
|
{
|
|
|
|
int j;
|
2009-07-10 09:44:08 +04:00
|
|
|
mt->state[0] = s & 0xffffffffU;
|
2002-07-26 10:12:39 +04:00
|
|
|
for (j=1; j<N; j++) {
|
2009-07-10 09:44:08 +04:00
|
|
|
mt->state[j] = (1812433253U * (mt->state[j-1] ^ (mt->state[j-1] >> 30)) + j);
|
2002-07-26 10:12:39 +04:00
|
|
|
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
|
|
|
|
/* In the previous versions, MSBs of the seed affect */
|
|
|
|
/* only MSBs of the array state[]. */
|
|
|
|
/* 2002/01/09 modified by Makoto Matsumoto */
|
2009-07-10 09:44:08 +04:00
|
|
|
mt->state[j] &= 0xffffffff; /* for >32 bit machines */
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
2008-08-17 06:43:20 +04:00
|
|
|
mt->left = 1;
|
|
|
|
mt->next = mt->state + N - 1;
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
|
|
|
|
2005-01-04 20:38:39 +03:00
|
|
|
/* initialize by an array with array-length */
|
|
|
|
/* init_key is the array for initializing keys */
|
|
|
|
/* key_length is its length */
|
|
|
|
/* slight change for C++, 2004/2/26 */
|
|
|
|
static void
|
2009-07-10 09:44:08 +04:00
|
|
|
init_by_array(struct MT *mt, unsigned int init_key[], int key_length)
|
2005-01-04 20:38:39 +03:00
|
|
|
{
|
|
|
|
int i, j, k;
|
2009-07-10 09:44:08 +04:00
|
|
|
init_genrand(mt, 19650218U);
|
2005-01-04 20:38:39 +03:00
|
|
|
i=1; j=0;
|
|
|
|
k = (N>key_length ? N : key_length);
|
|
|
|
for (; k; k--) {
|
2009-07-10 09:44:08 +04:00
|
|
|
mt->state[i] = (mt->state[i] ^ ((mt->state[i-1] ^ (mt->state[i-1] >> 30)) * 1664525U))
|
2005-01-04 20:38:39 +03:00
|
|
|
+ init_key[j] + j; /* non linear */
|
2009-07-10 09:44:08 +04:00
|
|
|
mt->state[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
|
2005-01-04 20:38:39 +03:00
|
|
|
i++; j++;
|
2008-08-17 06:43:20 +04:00
|
|
|
if (i>=N) { mt->state[0] = mt->state[N-1]; i=1; }
|
2005-01-04 20:38:39 +03:00
|
|
|
if (j>=key_length) j=0;
|
|
|
|
}
|
|
|
|
for (k=N-1; k; k--) {
|
2009-07-10 09:44:08 +04:00
|
|
|
mt->state[i] = (mt->state[i] ^ ((mt->state[i-1] ^ (mt->state[i-1] >> 30)) * 1566083941U))
|
2005-01-04 20:38:39 +03:00
|
|
|
- i; /* non linear */
|
2009-07-10 09:44:08 +04:00
|
|
|
mt->state[i] &= 0xffffffffU; /* for WORDSIZE > 32 machines */
|
2005-01-04 20:38:39 +03:00
|
|
|
i++;
|
2008-08-17 06:43:20 +04:00
|
|
|
if (i>=N) { mt->state[0] = mt->state[N-1]; i=1; }
|
2005-01-04 20:38:39 +03:00
|
|
|
}
|
|
|
|
|
2009-07-10 09:44:08 +04:00
|
|
|
mt->state[0] = 0x80000000U; /* MSB is 1; assuring non-zero initial array */
|
2005-01-04 20:38:39 +03:00
|
|
|
}
|
|
|
|
|
2002-07-26 10:12:39 +04:00
|
|
|
static void
|
2008-08-17 06:43:20 +04:00
|
|
|
next_state(struct MT *mt)
|
2002-07-26 10:12:39 +04:00
|
|
|
{
|
2009-07-10 09:44:08 +04:00
|
|
|
unsigned int *p = mt->state;
|
2002-07-26 10:12:39 +04:00
|
|
|
int j;
|
|
|
|
|
|
|
|
/* if init_genrand() has not been called, */
|
|
|
|
/* a default initial seed is used */
|
2009-07-10 09:44:08 +04:00
|
|
|
if (!genrand_initialized(mt)) init_genrand(mt, 5489U);
|
2008-08-17 06:43:20 +04:00
|
|
|
|
|
|
|
mt->left = N;
|
|
|
|
mt->next = mt->state;
|
2002-07-26 10:12:39 +04:00
|
|
|
|
2008-08-17 06:43:20 +04:00
|
|
|
for (j=N-M+1; --j; p++)
|
2002-07-26 10:12:39 +04:00
|
|
|
*p = p[M] ^ TWIST(p[0], p[1]);
|
|
|
|
|
2008-08-17 06:43:20 +04:00
|
|
|
for (j=M; --j; p++)
|
2002-07-26 10:12:39 +04:00
|
|
|
*p = p[M-N] ^ TWIST(p[0], p[1]);
|
|
|
|
|
2008-08-17 06:43:20 +04:00
|
|
|
*p = p[M-N] ^ TWIST(p[0], mt->state[0]);
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
|
|
|
|
2005-01-04 20:38:39 +03:00
|
|
|
/* generates a random number on [0,0xffffffff]-interval */
|
2009-07-10 09:44:08 +04:00
|
|
|
static unsigned int
|
2008-08-17 06:43:20 +04:00
|
|
|
genrand_int32(struct MT *mt)
|
2002-07-26 10:12:39 +04:00
|
|
|
{
|
2009-07-10 09:44:08 +04:00
|
|
|
unsigned int y;
|
2002-07-26 10:12:39 +04:00
|
|
|
|
2008-08-17 06:43:20 +04:00
|
|
|
if (--mt->left <= 0) next_state(mt);
|
|
|
|
y = *mt->next++;
|
2002-07-26 10:12:39 +04:00
|
|
|
|
|
|
|
/* Tempering */
|
|
|
|
y ^= (y >> 11);
|
2009-07-10 09:44:08 +04:00
|
|
|
y ^= (y << 7) & 0x9d2c5680;
|
|
|
|
y ^= (y << 15) & 0xefc60000;
|
2002-07-26 10:12:39 +04:00
|
|
|
y ^= (y >> 18);
|
|
|
|
|
2005-01-04 20:38:39 +03:00
|
|
|
return y;
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
|
|
|
|
2005-01-04 20:38:39 +03:00
|
|
|
/* generates a random number on [0,1) with 53-bit resolution*/
|
2007-12-24 11:19:28 +03:00
|
|
|
static double
|
2008-08-17 06:43:20 +04:00
|
|
|
genrand_real(struct MT *mt)
|
|
|
|
{
|
2009-07-10 09:44:08 +04:00
|
|
|
unsigned int a = genrand_int32(mt)>>5, b = genrand_int32(mt)>>6;
|
2008-08-17 06:43:20 +04:00
|
|
|
return(a*67108864.0+b)*(1.0/9007199254740992.0);
|
|
|
|
}
|
2005-01-04 20:38:39 +03:00
|
|
|
/* These real versions are due to Isaku Wada, 2002/01/09 added */
|
|
|
|
|
2002-07-26 10:56:30 +04:00
|
|
|
#undef N
|
|
|
|
#undef M
|
|
|
|
|
2002-07-26 10:12:39 +04:00
|
|
|
/* These real versions are due to Isaku Wada, 2002/01/09 added */
|
|
|
|
|
2007-06-10 07:06:15 +04:00
|
|
|
#include "ruby/ruby.h"
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-09-01 13:48:03 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
#include <time.h>
|
2005-01-03 05:41:03 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2008-08-17 06:43:20 +04:00
|
|
|
#define DEFAULT_SEED_CNT 4
|
|
|
|
|
|
|
|
struct RandSeed {
|
|
|
|
VALUE value;
|
2009-07-10 09:44:08 +04:00
|
|
|
unsigned int initial[DEFAULT_SEED_CNT];
|
2008-08-17 06:43:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Random {
|
|
|
|
struct MT mt;
|
|
|
|
struct RandSeed seed;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct Random default_mt;
|
|
|
|
|
2007-12-24 11:19:28 +03:00
|
|
|
unsigned long
|
|
|
|
rb_genrand_int32(void)
|
|
|
|
{
|
2008-08-17 06:43:20 +04:00
|
|
|
return genrand_int32(&default_mt.mt);
|
2007-12-24 11:19:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
rb_genrand_real(void)
|
|
|
|
{
|
2008-08-17 06:43:20 +04:00
|
|
|
return genrand_real(&default_mt.mt);
|
2007-12-24 11:19:28 +03:00
|
|
|
}
|
|
|
|
|
2009-07-10 09:44:08 +04:00
|
|
|
#define SIZEOF_INT32 (31/CHAR_BIT + 1)
|
|
|
|
|
2005-01-04 20:38:39 +03:00
|
|
|
static VALUE
|
2008-08-17 06:43:20 +04:00
|
|
|
rand_init(struct MT *mt, VALUE vseed)
|
2000-01-08 08:00:25 +03:00
|
|
|
{
|
2005-01-04 20:38:39 +03:00
|
|
|
volatile VALUE seed;
|
2009-07-10 09:44:08 +04:00
|
|
|
long blen = 0;
|
|
|
|
int len;
|
|
|
|
unsigned int *buf;
|
2000-01-08 08:00:25 +03:00
|
|
|
|
2005-01-04 20:38:39 +03:00
|
|
|
seed = rb_to_int(vseed);
|
|
|
|
switch (TYPE(seed)) {
|
|
|
|
case T_FIXNUM:
|
2009-07-10 09:44:08 +04:00
|
|
|
len = (int)sizeof(VALUE);
|
|
|
|
break;
|
2005-01-04 20:38:39 +03:00
|
|
|
case T_BIGNUM:
|
2009-07-10 09:44:08 +04:00
|
|
|
blen = RBIGNUM_LEN(seed);
|
|
|
|
if (blen == 0)
|
|
|
|
len = 4;
|
|
|
|
else if (blen > MT_MAX_STATE * SIZEOF_INT32 / SIZEOF_BDIGITS)
|
|
|
|
blen = (len = MT_MAX_STATE) * SIZEOF_INT32 / SIZEOF_BDIGITS;
|
|
|
|
else
|
|
|
|
len = (int)blen * SIZEOF_BDIGITS;
|
|
|
|
break;
|
2005-01-04 20:38:39 +03:00
|
|
|
default:
|
2009-07-10 09:44:08 +04:00
|
|
|
rb_raise(rb_eTypeError, "failed to convert %s into Integer",
|
|
|
|
rb_obj_classname(vseed));
|
2005-01-04 20:38:39 +03:00
|
|
|
}
|
2005-01-05 14:31:41 +03:00
|
|
|
len = (len + 3) / 4; /* number of 32bit words */
|
2009-07-10 09:44:08 +04:00
|
|
|
buf = ALLOC_N(unsigned int, len); /* allocate longs for init_by_array */
|
2009-07-10 19:16:32 +04:00
|
|
|
memset(buf, 0, len * sizeof(int));
|
2005-01-04 20:38:39 +03:00
|
|
|
if (FIXNUM_P(seed)) {
|
2009-07-10 19:16:32 +04:00
|
|
|
buf[0] = (unsigned int)(FIX2ULONG(seed) & 0xffffffff);
|
2005-01-04 20:38:39 +03:00
|
|
|
#if SIZEOF_LONG > 4
|
2009-07-10 19:16:32 +04:00
|
|
|
buf[1] = (unsigned int)(FIX2ULONG(seed) >> 32);
|
2005-01-04 20:38:39 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
2009-07-10 09:44:08 +04:00
|
|
|
long i, j;
|
|
|
|
for (i = blen-1; 0 <= i; i--) {
|
2005-01-04 20:38:39 +03:00
|
|
|
j = i * SIZEOF_BDIGITS / 4;
|
2005-01-05 14:31:41 +03:00
|
|
|
#if SIZEOF_BDIGITS < 4
|
|
|
|
buf[j] <<= SIZEOF_BDIGITS * 8;
|
2005-01-04 20:38:39 +03:00
|
|
|
#endif
|
2007-09-01 16:02:36 +04:00
|
|
|
buf[j] |= RBIGNUM_DIGITS(seed)[i];
|
2005-01-04 20:38:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (1 < len && buf[len-1] == 0) {
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
if (len <= 1) {
|
2008-08-17 06:43:20 +04:00
|
|
|
init_genrand(mt, buf[0]);
|
2005-01-04 20:38:39 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (buf[len-1] == 1) /* remove leading-zero-guard */
|
|
|
|
len--;
|
2008-08-17 06:43:20 +04:00
|
|
|
init_by_array(mt, buf, len);
|
2005-01-04 20:38:39 +03:00
|
|
|
}
|
* array.c, bignum.c, cont.c, dir.c, dln.c, encoding.c, enumerator.c,
enumerator.c (enumerator_allocate), eval_jump.c, file.c, hash.c,
io.c, load.c, pack.c, proc.c, random.c, re.c, ruby.c, st.c,
string.c, thread.c, thread_pthread.c, time.c, util.c, variable.c,
vm.c, gc.c:
allocated memory objects by xmalloc (ruby_xmalloc) should be
freed by xfree (ruby_xfree).
* ext/curses/curses.c, ext/dbm/dbm.c, ext/digest/digest.c,
ext/gdbm/gdbm.c, ext/json/ext/parser/parser.c,
ext/json/ext/parser/unicode.c, ext/openssl/ossl_cipher.c,
ext/openssl/ossl_hmac.c, ext/openssl/ossl_pkey_ec.c,
ext/sdbm/init.c, ext/strscan/strscan.c, ext/zlib/zlib.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17017 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-06-08 14:01:40 +04:00
|
|
|
xfree(buf);
|
2008-08-17 06:43:20 +04:00
|
|
|
return seed;
|
2000-01-08 08:00:25 +03:00
|
|
|
}
|
|
|
|
|
2009-07-10 09:44:08 +04:00
|
|
|
#define DEFAULT_SEED_LEN (DEFAULT_SEED_CNT * sizeof(int))
|
2008-06-20 06:46:17 +04:00
|
|
|
|
|
|
|
static void
|
2009-07-10 09:44:08 +04:00
|
|
|
fill_random_seed(unsigned int seed[DEFAULT_SEED_CNT])
|
2002-07-26 10:12:39 +04:00
|
|
|
{
|
|
|
|
static int n = 0;
|
|
|
|
struct timeval tv;
|
2009-03-11 23:14:51 +03:00
|
|
|
#ifdef S_ISCHR
|
2005-01-03 05:41:03 +03:00
|
|
|
int fd;
|
2005-01-03 08:13:18 +03:00
|
|
|
struct stat statbuf;
|
2009-03-11 23:14:51 +03:00
|
|
|
#endif
|
2005-01-05 14:31:41 +03:00
|
|
|
|
2008-08-17 06:43:20 +04:00
|
|
|
memset(seed, 0, DEFAULT_SEED_LEN);
|
2005-01-03 05:41:03 +03:00
|
|
|
|
2005-01-03 08:13:18 +03:00
|
|
|
#ifdef S_ISCHR
|
2005-01-06 11:24:04 +03:00
|
|
|
if ((fd = open("/dev/urandom", O_RDONLY
|
|
|
|
#ifdef O_NONBLOCK
|
|
|
|
|O_NONBLOCK
|
|
|
|
#endif
|
2005-01-03 08:13:18 +03:00
|
|
|
#ifdef O_NOCTTY
|
|
|
|
|O_NOCTTY
|
|
|
|
#endif
|
|
|
|
#ifdef O_NOFOLLOW
|
|
|
|
|O_NOFOLLOW
|
|
|
|
#endif
|
|
|
|
)) >= 0) {
|
|
|
|
if (fstat(fd, &statbuf) == 0 && S_ISCHR(statbuf.st_mode)) {
|
2009-02-18 09:21:04 +03:00
|
|
|
(void)read(fd, seed, DEFAULT_SEED_LEN);
|
2005-01-03 08:13:18 +03:00
|
|
|
}
|
2005-01-03 05:41:03 +03:00
|
|
|
close(fd);
|
|
|
|
}
|
2005-01-03 08:13:18 +03:00
|
|
|
#endif
|
2005-01-03 05:41:03 +03:00
|
|
|
|
2005-01-05 14:31:41 +03:00
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
seed[0] ^= tv.tv_usec;
|
2009-07-10 09:44:08 +04:00
|
|
|
seed[1] ^= (unsigned int)tv.tv_sec;
|
|
|
|
#if SIZEOF_TIME_T > SIZEOF_INT
|
2009-07-10 19:16:32 +04:00
|
|
|
seed[0] ^= (unsigned int)(tv.tv_sec >> SIZEOF_INT * CHAR_BIT);
|
2009-07-10 09:44:08 +04:00
|
|
|
#endif
|
2005-01-05 14:31:41 +03:00
|
|
|
seed[2] ^= getpid() ^ (n++ << 16);
|
2009-07-10 09:44:08 +04:00
|
|
|
seed[3] ^= (unsigned int)(VALUE)&seed;
|
|
|
|
#if SIZEOF_VOIDP > SIZEOF_INT
|
|
|
|
seed[2] ^= (unsigned int)((VALUE)&seed >> SIZEOF_INT * CHAR_BIT);
|
|
|
|
#endif
|
2008-06-20 06:46:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2008-08-17 06:43:20 +04:00
|
|
|
make_seed_value(const void *ptr)
|
2008-06-20 06:46:17 +04:00
|
|
|
{
|
|
|
|
BDIGIT *digits;
|
|
|
|
NEWOBJ(big, struct RBignum);
|
|
|
|
OBJSETUP(big, rb_cBignum, T_BIGNUM);
|
|
|
|
|
|
|
|
RBIGNUM_SET_SIGN(big, 1);
|
|
|
|
rb_big_resize((VALUE)big, DEFAULT_SEED_LEN / SIZEOF_BDIGITS + 1);
|
|
|
|
digits = RBIGNUM_DIGITS(big);
|
|
|
|
|
2009-07-10 09:44:08 +04:00
|
|
|
MEMCPY(digits, ptr, char, DEFAULT_SEED_LEN);
|
2005-01-05 14:31:41 +03:00
|
|
|
|
2005-01-04 20:38:39 +03:00
|
|
|
/* set leading-zero-guard if need. */
|
2007-09-01 16:02:36 +04:00
|
|
|
digits[RBIGNUM_LEN(big)-1] = digits[RBIGNUM_LEN(big)-2] <= 1 ? 1 : 0;
|
2005-01-04 20:38:39 +03:00
|
|
|
|
|
|
|
return rb_big_norm((VALUE)big);
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
|
|
|
|
2008-06-20 06:46:17 +04:00
|
|
|
static VALUE
|
|
|
|
random_seed(void)
|
|
|
|
{
|
2009-07-10 09:44:08 +04:00
|
|
|
unsigned int buf[DEFAULT_SEED_CNT];
|
2008-06-20 06:46:17 +04:00
|
|
|
fill_random_seed(buf);
|
|
|
|
return make_seed_value(buf);
|
|
|
|
}
|
|
|
|
|
2003-12-28 23:47:56 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* srand(number=0) => old_seed
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2003-12-28 23:47:56 +03:00
|
|
|
* Seeds the pseudorandom number generator to the value of
|
2009-01-05 02:00:39 +03:00
|
|
|
* <i>number</i>. If <i>number</i> is omitted
|
2003-12-28 23:47:56 +03:00
|
|
|
* or zero, seeds the generator using a combination of the time, the
|
|
|
|
* process id, and a sequence number. (This is also the behavior if
|
|
|
|
* <code>Kernel::rand</code> is called without previously calling
|
|
|
|
* <code>srand</code>, but without the sequence.) By setting the seed
|
|
|
|
* to a known value, scripts can be made deterministic during testing.
|
|
|
|
* The previous seed value is returned. Also see <code>Kernel::rand</code>.
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_f_srand(int argc, VALUE *argv, VALUE obj)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2005-01-04 20:38:39 +03:00
|
|
|
VALUE seed, old;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-11-14 10:10:31 +03:00
|
|
|
rb_secure(4);
|
2008-03-05 08:22:17 +03:00
|
|
|
if (argc == 0) {
|
2002-07-26 10:12:39 +04:00
|
|
|
seed = random_seed();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2008-03-05 08:22:17 +03:00
|
|
|
else {
|
|
|
|
rb_scan_args(argc, argv, "01", &seed);
|
|
|
|
}
|
2008-08-17 06:43:20 +04:00
|
|
|
old = default_mt.seed.value;
|
|
|
|
default_mt.seed.value = rand_init(&default_mt.mt, seed);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2005-01-04 20:38:39 +03:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2009-02-22 17:23:33 +03:00
|
|
|
static unsigned long
|
2005-01-04 20:38:39 +03:00
|
|
|
make_mask(unsigned long x)
|
|
|
|
{
|
|
|
|
x = x | x >> 1;
|
|
|
|
x = x | x >> 2;
|
|
|
|
x = x | x >> 4;
|
|
|
|
x = x | x >> 8;
|
|
|
|
x = x | x >> 16;
|
|
|
|
#if 4 < SIZEOF_LONG
|
|
|
|
x = x | x >> 32;
|
|
|
|
#endif
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long
|
2008-08-17 06:43:20 +04:00
|
|
|
limited_rand(struct MT *mt, unsigned long limit)
|
2005-01-04 20:38:39 +03:00
|
|
|
{
|
|
|
|
unsigned long mask = make_mask(limit);
|
|
|
|
int i;
|
|
|
|
unsigned long val;
|
|
|
|
|
|
|
|
retry:
|
|
|
|
val = 0;
|
|
|
|
for (i = SIZEOF_LONG/4-1; 0 <= i; i--) {
|
|
|
|
if (mask >> (i * 32)) {
|
2008-08-17 06:43:20 +04:00
|
|
|
val |= genrand_int32(mt) << (i * 32);
|
2005-01-04 20:38:39 +03:00
|
|
|
val &= mask;
|
|
|
|
if (limit < val)
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2008-08-17 06:43:20 +04:00
|
|
|
limited_big_rand(struct MT *mt, struct RBignum *limit)
|
2005-01-04 20:38:39 +03:00
|
|
|
{
|
|
|
|
unsigned long mask, lim, rnd;
|
|
|
|
struct RBignum *val;
|
2009-07-10 09:44:08 +04:00
|
|
|
long i, len;
|
|
|
|
int boundary;
|
2005-01-04 20:38:39 +03:00
|
|
|
|
2007-09-01 16:02:36 +04:00
|
|
|
len = (RBIGNUM_LEN(limit) * SIZEOF_BDIGITS + 3) / 4;
|
2005-01-04 20:38:39 +03:00
|
|
|
val = (struct RBignum *)rb_big_clone((VALUE)limit);
|
2007-09-01 16:02:36 +04:00
|
|
|
RBIGNUM_SET_SIGN(val, 1);
|
2005-01-04 20:38:39 +03:00
|
|
|
#if SIZEOF_BDIGITS == 2
|
2008-02-02 03:32:40 +03:00
|
|
|
# define BIG_GET32(big,i) \
|
|
|
|
(RBIGNUM_DIGITS(big)[(i)*2] | \
|
|
|
|
((i)*2+1 < RBIGNUM_LEN(big) ? \
|
|
|
|
(RBIGNUM_DIGITS(big)[(i)*2+1] << 16) : \
|
|
|
|
0))
|
|
|
|
# define BIG_SET32(big,i,d) \
|
|
|
|
((RBIGNUM_DIGITS(big)[(i)*2] = (d) & 0xffff), \
|
|
|
|
((i)*2+1 < RBIGNUM_LEN(big) ? \
|
|
|
|
(RBIGNUM_DIGITS(big)[(i)*2+1] = (d) >> 16) : \
|
|
|
|
0))
|
2005-01-04 20:38:39 +03:00
|
|
|
#else
|
|
|
|
/* SIZEOF_BDIGITS == 4 */
|
2007-09-01 16:02:36 +04:00
|
|
|
# define BIG_GET32(big,i) (RBIGNUM_DIGITS(big)[i])
|
|
|
|
# define BIG_SET32(big,i,d) (RBIGNUM_DIGITS(big)[i] = (d))
|
2005-01-04 20:38:39 +03:00
|
|
|
#endif
|
|
|
|
retry:
|
|
|
|
mask = 0;
|
|
|
|
boundary = 1;
|
|
|
|
for (i = len-1; 0 <= i; i--) {
|
|
|
|
lim = BIG_GET32(limit, i);
|
|
|
|
mask = mask ? 0xffffffff : make_mask(lim);
|
|
|
|
if (mask) {
|
2008-08-17 06:43:20 +04:00
|
|
|
rnd = genrand_int32(mt) & mask;
|
2005-01-04 20:38:39 +03:00
|
|
|
if (boundary) {
|
|
|
|
if (lim < rnd)
|
|
|
|
goto retry;
|
|
|
|
if (rnd < lim)
|
|
|
|
boundary = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rnd = 0;
|
|
|
|
}
|
2009-07-10 09:44:08 +04:00
|
|
|
BIG_SET32(val, i, (BDIGIT)rnd);
|
2005-01-04 20:38:39 +03:00
|
|
|
}
|
|
|
|
return rb_big_norm((VALUE)val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2008-12-14 06:59:02 +03:00
|
|
|
unsigned long
|
|
|
|
rb_rand_internal(unsigned long i)
|
|
|
|
{
|
|
|
|
struct MT *mt = &default_mt.mt;
|
|
|
|
if (!genrand_initialized(mt)) {
|
|
|
|
rand_init(mt, random_seed());
|
|
|
|
}
|
|
|
|
return limited_rand(mt, i);
|
|
|
|
}
|
|
|
|
|
2003-12-28 23:47:56 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* rand(max=0) => number
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2003-12-28 23:47:56 +03:00
|
|
|
* Converts <i>max</i> to an integer using max1 =
|
|
|
|
* max<code>.to_i.abs</code>. If the result is zero, returns a
|
|
|
|
* pseudorandom floating point number greater than or equal to 0.0 and
|
|
|
|
* less than 1.0. Otherwise, returns a pseudorandom integer greater
|
|
|
|
* than or equal to zero and less than max1. <code>Kernel::srand</code>
|
|
|
|
* may be used to ensure repeatable sequences of random numbers between
|
|
|
|
* different runs of the program. Ruby currently uses a modified
|
2006-09-25 03:14:14 +04:00
|
|
|
* Mersenne Twister with a period of 2**19937-1.
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2003-12-28 23:47:56 +03:00
|
|
|
* srand 1234 #=> 0
|
|
|
|
* [ rand, rand ] #=> [0.191519450163469, 0.49766366626136]
|
|
|
|
* [ rand(10), rand(1000) ] #=> [6, 817]
|
|
|
|
* srand 1234 #=> 1234
|
|
|
|
* [ rand, rand ] #=> [0.191519450163469, 0.49766366626136]
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
rb_f_rand(int argc, VALUE *argv, VALUE obj)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-07-04 08:17:26 +04:00
|
|
|
VALUE vmax;
|
1999-01-20 07:59:39 +03:00
|
|
|
long val, max;
|
2008-08-17 06:43:20 +04:00
|
|
|
struct MT *mt = &default_mt.mt;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-08-17 06:43:20 +04:00
|
|
|
if (!genrand_initialized(mt)) {
|
|
|
|
rand_init(mt, random_seed());
|
2008-06-20 06:46:17 +04:00
|
|
|
}
|
2008-12-29 16:29:12 +03:00
|
|
|
if (argc == 0) goto zero_arg;
|
|
|
|
rb_scan_args(argc, argv, "01", &vmax);
|
|
|
|
if (NIL_P(vmax)) goto zero_arg;
|
|
|
|
vmax = rb_to_int(vmax);
|
|
|
|
if (TYPE(vmax) == T_BIGNUM) {
|
|
|
|
struct RBignum *limit = (struct RBignum *)vmax;
|
|
|
|
if (!RBIGNUM_SIGN(limit)) {
|
|
|
|
limit = (struct RBignum *)rb_big_clone(vmax);
|
|
|
|
RBIGNUM_SET_SIGN(limit, 1);
|
2000-11-02 12:04:54 +03:00
|
|
|
}
|
2008-12-29 16:29:12 +03:00
|
|
|
limit = (struct RBignum *)rb_big_minus((VALUE)limit, INT2FIX(1));
|
|
|
|
if (FIXNUM_P((VALUE)limit)) {
|
|
|
|
if (FIX2LONG((VALUE)limit) == -1)
|
|
|
|
return DBL2NUM(genrand_real(mt));
|
|
|
|
return LONG2NUM(limited_rand(mt, FIX2LONG((VALUE)limit)));
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
2008-12-29 16:29:12 +03:00
|
|
|
return limited_big_rand(mt, limit);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2008-12-29 16:29:12 +03:00
|
|
|
max = NUM2LONG(vmax);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (max == 0) {
|
2008-12-29 16:29:12 +03:00
|
|
|
zero_arg:
|
2008-09-05 22:24:21 +04:00
|
|
|
return DBL2NUM(genrand_real(mt));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2002-07-26 10:12:39 +04:00
|
|
|
if (max < 0) max = -max;
|
2008-08-17 06:43:20 +04:00
|
|
|
val = limited_rand(mt, max-1);
|
2002-04-25 17:57:01 +04:00
|
|
|
return LONG2NUM(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2008-06-20 06:46:17 +04:00
|
|
|
void
|
|
|
|
Init_RandomSeed(void)
|
|
|
|
{
|
2008-08-17 06:43:20 +04:00
|
|
|
fill_random_seed(default_mt.seed.initial);
|
|
|
|
init_by_array(&default_mt.mt, default_mt.seed.initial, DEFAULT_SEED_CNT);
|
2008-06-20 06:46:17 +04:00
|
|
|
}
|
|
|
|
|
2008-06-20 07:29:19 +04:00
|
|
|
static void
|
2008-06-20 06:46:17 +04:00
|
|
|
Init_RandomSeed2(void)
|
|
|
|
{
|
2008-08-17 06:43:20 +04:00
|
|
|
default_mt.seed.value = make_seed_value(default_mt.seed.initial);
|
|
|
|
memset(default_mt.seed.initial, 0, DEFAULT_SEED_LEN);
|
2008-06-20 06:46:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_reset_random_seed(void)
|
|
|
|
{
|
2008-08-17 06:43:20 +04:00
|
|
|
uninit_genrand(&default_mt.mt);
|
|
|
|
default_mt.seed.value = INT2FIX(0);
|
2008-06-20 06:46:17 +04:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
Init_Random(void)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2008-06-20 06:46:17 +04:00
|
|
|
Init_RandomSeed2();
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_global_function("srand", rb_f_srand, -1);
|
2000-07-04 08:17:26 +04:00
|
|
|
rb_define_global_function("rand", rb_f_rand, -1);
|
2008-08-17 06:43:20 +04:00
|
|
|
rb_global_variable(&default_mt.seed.value);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|