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
|
|
|
|
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.
|
|
|
|
|
|
|
|
Before using, initialize the state by using init_genrand(seed)
|
|
|
|
or init_by_array(init_key, key_length).
|
|
|
|
|
|
|
|
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
3. The names of its contributors may not be used to endorse or promote
|
|
|
|
products derived from this software without specific prior written
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Period parameters */
|
|
|
|
#define N 624
|
|
|
|
#define M 397
|
|
|
|
#define MATRIX_A 0x9908b0dfUL /* constant vector a */
|
|
|
|
#define UMASK 0x80000000UL /* most significant w-r bits */
|
|
|
|
#define LMASK 0x7fffffffUL /* least significant r bits */
|
|
|
|
#define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
|
|
|
|
#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
|
|
|
|
|
|
|
|
static unsigned long state[N]; /* the array for the state vector */
|
|
|
|
static int left = 1;
|
|
|
|
static int initf = 0;
|
|
|
|
static unsigned long *next;
|
|
|
|
|
|
|
|
/* initializes state[N] with a seed */
|
|
|
|
static 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_genrand(unsigned long s)
|
2002-07-26 10:12:39 +04:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
state[0]= s & 0xffffffffUL;
|
|
|
|
for (j=1; j<N; j++) {
|
|
|
|
state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j);
|
|
|
|
/* 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 */
|
|
|
|
state[j] &= 0xffffffffUL; /* for >32 bit machines */
|
|
|
|
}
|
|
|
|
left = 1; initf = 1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
init_by_array(unsigned long init_key[], int key_length)
|
|
|
|
{
|
|
|
|
int i, j, k;
|
|
|
|
init_genrand(19650218UL);
|
|
|
|
i=1; j=0;
|
|
|
|
k = (N>key_length ? N : key_length);
|
|
|
|
for (; k; k--) {
|
|
|
|
state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1664525UL))
|
|
|
|
+ init_key[j] + j; /* non linear */
|
|
|
|
state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
|
|
|
|
i++; j++;
|
|
|
|
if (i>=N) { state[0] = state[N-1]; i=1; }
|
|
|
|
if (j>=key_length) j=0;
|
|
|
|
}
|
|
|
|
for (k=N-1; k; k--) {
|
|
|
|
state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL))
|
|
|
|
- i; /* non linear */
|
|
|
|
state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
|
|
|
|
i++;
|
|
|
|
if (i>=N) { state[0] = state[N-1]; i=1; }
|
|
|
|
}
|
|
|
|
|
|
|
|
state[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
|
|
|
|
left = 1; initf = 1;
|
|
|
|
}
|
|
|
|
|
2002-07-26 10:12:39 +04:00
|
|
|
static 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
|
|
|
next_state(void)
|
2002-07-26 10:12:39 +04:00
|
|
|
{
|
|
|
|
unsigned long *p=state;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/* if init_genrand() has not been called, */
|
|
|
|
/* a default initial seed is used */
|
|
|
|
if (initf==0) init_genrand(5489UL);
|
|
|
|
|
|
|
|
left = N;
|
|
|
|
next = state;
|
|
|
|
|
|
|
|
for (j=N-M+1; --j; p++)
|
|
|
|
*p = p[M] ^ TWIST(p[0], p[1]);
|
|
|
|
|
|
|
|
for (j=M; --j; p++)
|
|
|
|
*p = p[M-N] ^ TWIST(p[0], p[1]);
|
|
|
|
|
|
|
|
*p = p[M-N] ^ TWIST(p[0], state[0]);
|
|
|
|
}
|
|
|
|
|
2005-01-04 20:38:39 +03:00
|
|
|
/* generates a random number on [0,0xffffffff]-interval */
|
2007-12-24 11:19:28 +03:00
|
|
|
static unsigned long
|
2005-01-04 20:38:39 +03:00
|
|
|
genrand_int32(void)
|
2002-07-26 10:12:39 +04:00
|
|
|
{
|
|
|
|
unsigned long y;
|
|
|
|
|
|
|
|
if (--left == 0) next_state();
|
|
|
|
y = *next++;
|
|
|
|
|
|
|
|
/* Tempering */
|
|
|
|
y ^= (y >> 11);
|
|
|
|
y ^= (y << 7) & 0x9d2c5680UL;
|
|
|
|
y ^= (y << 15) & 0xefc60000UL;
|
|
|
|
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
|
* 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
|
|
|
genrand_real(void)
|
2005-01-04 20:38:39 +03:00
|
|
|
{
|
|
|
|
unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
|
|
|
|
return(a*67108864.0+b)*(1.0/9007199254740992.0);
|
|
|
|
}
|
|
|
|
/* 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
|
|
|
|
2007-12-24 11:19:28 +03:00
|
|
|
unsigned long
|
|
|
|
rb_genrand_int32(void)
|
|
|
|
{
|
|
|
|
return genrand_int32();
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
rb_genrand_real(void)
|
|
|
|
{
|
|
|
|
return genrand_real();
|
|
|
|
}
|
|
|
|
|
2005-01-04 20:38:39 +03:00
|
|
|
static VALUE saved_seed = INT2FIX(0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2005-01-04 20:38:39 +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
|
|
|
rand_init(VALUE vseed)
|
2000-01-08 08:00:25 +03:00
|
|
|
{
|
2005-01-04 20:38:39 +03:00
|
|
|
volatile VALUE seed;
|
|
|
|
VALUE old;
|
|
|
|
long len;
|
|
|
|
unsigned long *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:
|
|
|
|
len = sizeof(VALUE);
|
|
|
|
break;
|
|
|
|
case T_BIGNUM:
|
2007-09-01 16:02:36 +04:00
|
|
|
len = RBIGNUM_LEN(seed) * SIZEOF_BDIGITS;
|
2005-01-04 20:38:39 +03:00
|
|
|
if (len == 0)
|
|
|
|
len = 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eTypeError, "failed to convert %s into Integer",
|
|
|
|
rb_obj_classname(vseed));
|
|
|
|
}
|
2005-01-05 14:31:41 +03:00
|
|
|
len = (len + 3) / 4; /* number of 32bit words */
|
2005-02-12 09:07:47 +03:00
|
|
|
buf = ALLOC_N(unsigned long, len); /* allocate longs for init_by_array */
|
2005-01-04 20:38:39 +03:00
|
|
|
memset(buf, 0, len * sizeof(long));
|
|
|
|
if (FIXNUM_P(seed)) {
|
|
|
|
buf[0] = FIX2ULONG(seed) & 0xffffffff;
|
|
|
|
#if SIZEOF_LONG > 4
|
|
|
|
buf[1] = FIX2ULONG(seed) >> 32;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int i, j;
|
2007-09-01 16:02:36 +04:00
|
|
|
for (i = RBIGNUM_LEN(seed)-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) {
|
|
|
|
init_genrand(buf[0]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (buf[len-1] == 1) /* remove leading-zero-guard */
|
|
|
|
len--;
|
|
|
|
init_by_array(buf, len);
|
|
|
|
}
|
2000-01-08 08:00:25 +03:00
|
|
|
old = saved_seed;
|
|
|
|
saved_seed = seed;
|
2005-01-07 17:04:30 +03:00
|
|
|
free(buf);
|
2000-01-08 08:00:25 +03:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2005-01-04 20:38:39 +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
|
|
|
random_seed(void)
|
2002-07-26 10:12:39 +04:00
|
|
|
{
|
|
|
|
static int n = 0;
|
|
|
|
struct timeval tv;
|
2005-01-03 05:41:03 +03:00
|
|
|
int fd;
|
2005-01-03 08:13:18 +03:00
|
|
|
struct stat statbuf;
|
2005-01-05 14:31:41 +03:00
|
|
|
|
|
|
|
int seed_len;
|
2005-01-04 20:38:39 +03:00
|
|
|
BDIGIT *digits;
|
|
|
|
unsigned long *seed;
|
|
|
|
NEWOBJ(big, struct RBignum);
|
|
|
|
OBJSETUP(big, rb_cBignum, T_BIGNUM);
|
2005-01-05 14:31:41 +03:00
|
|
|
|
|
|
|
seed_len = 4 * sizeof(long);
|
2007-09-01 16:02:36 +04:00
|
|
|
RBIGNUM_SET_SIGN(big, 1);
|
|
|
|
rb_big_resize((VALUE)big, seed_len / SIZEOF_BDIGITS + 1);
|
|
|
|
digits = RBIGNUM_DIGITS(big);
|
|
|
|
seed = (unsigned long *)RBIGNUM_DIGITS(big);
|
2002-07-26 10:12:39 +04:00
|
|
|
|
2007-09-01 16:02:36 +04:00
|
|
|
memset(digits, 0, RBIGNUM_LEN(big) * SIZEOF_BDIGITS);
|
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)) {
|
2005-01-05 14:31:41 +03:00
|
|
|
read(fd, seed, 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;
|
|
|
|
seed[1] ^= tv.tv_sec;
|
|
|
|
seed[2] ^= getpid() ^ (n++ << 16);
|
|
|
|
seed[3] ^= (unsigned long)&seed;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-12-28 23:47:56 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* srand(number=0) => old_seed
|
|
|
|
*
|
|
|
|
* Seeds the pseudorandom number generator to the value of
|
|
|
|
* <i>number</i>.<code>to_i.abs</code>. If <i>number</i> is omitted
|
|
|
|
* 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);
|
2005-01-04 20:38:39 +03:00
|
|
|
if (rb_scan_args(argc, argv, "01", &seed) == 0) {
|
2002-07-26 10:12:39 +04:00
|
|
|
seed = random_seed();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-01-08 08:00:25 +03:00
|
|
|
old = rand_init(seed);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2005-01-04 20:38:39 +03:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long
|
|
|
|
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
|
|
|
|
limited_rand(unsigned long limit)
|
|
|
|
{
|
|
|
|
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)) {
|
|
|
|
val |= genrand_int32() << (i * 32);
|
|
|
|
val &= mask;
|
|
|
|
if (limit < val)
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
limited_big_rand(struct RBignum *limit)
|
|
|
|
{
|
|
|
|
unsigned long mask, lim, rnd;
|
|
|
|
struct RBignum *val;
|
|
|
|
int i, len, boundary;
|
|
|
|
|
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
|
2007-09-01 16:02:36 +04:00
|
|
|
# define BIG_GET32(big,i) (RBIGNUM_DIGITS(big)[(i)*2] | \
|
|
|
|
((i)*2+1 < RBIGNUM_DIGITS(big) ? (RBIGNUM_DIGITS(big)[(i)*2+1] << 16) \
|
2005-01-04 20:38:39 +03:00
|
|
|
: 0))
|
2007-09-01 16:02:36 +04:00
|
|
|
# define BIG_SET32(big,i,d) ((RBIGNUM_DIGITS(big)[(i)*2] = (d) & 0xffff), \
|
|
|
|
((i)*2+1 < RBIGNUM_DIGITS(big) ? (RBIGNUM_DIGITS(big)[(i)*2+1] = (d) >> 16) \
|
2005-01-04 20:38:39 +03:00
|
|
|
: 0))
|
|
|
|
#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) {
|
|
|
|
rnd = genrand_int32() & mask;
|
|
|
|
if (boundary) {
|
|
|
|
if (lim < rnd)
|
|
|
|
goto retry;
|
|
|
|
if (rnd < lim)
|
|
|
|
boundary = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rnd = 0;
|
|
|
|
}
|
|
|
|
BIG_SET32(val, i, rnd);
|
|
|
|
}
|
|
|
|
return rb_big_norm((VALUE)val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2003-12-28 23:47:56 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* rand(max=0) => number
|
|
|
|
*
|
|
|
|
* 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.
|
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;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-07-04 08:17:26 +04:00
|
|
|
rb_scan_args(argc, argv, "01", &vmax);
|
1998-01-16 15:13:05 +03:00
|
|
|
switch (TYPE(vmax)) {
|
|
|
|
case T_FLOAT:
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
if (RFLOAT_VALUE(vmax) <= LONG_MAX && RFLOAT_VALUE(vmax) >= LONG_MIN) {
|
|
|
|
max = (long)RFLOAT_VALUE(vmax);
|
1999-12-06 12:04:03 +03:00
|
|
|
break;
|
2000-11-02 12:04:54 +03:00
|
|
|
}
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
if (RFLOAT_VALUE(vmax) < 0)
|
|
|
|
vmax = rb_dbl2big(-RFLOAT_VALUE(vmax));
|
2005-01-04 20:38:39 +03:00
|
|
|
else
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
vmax = rb_dbl2big(RFLOAT_VALUE(vmax));
|
1999-12-06 12:04:03 +03:00
|
|
|
/* fall through */
|
2003-01-31 06:15:33 +03:00
|
|
|
case T_BIGNUM:
|
|
|
|
bignum:
|
2002-07-26 10:12:39 +04:00
|
|
|
{
|
2005-01-04 20:38:39 +03:00
|
|
|
struct RBignum *limit = (struct RBignum *)vmax;
|
2007-09-01 16:02:36 +04:00
|
|
|
if (!RBIGNUM_SIGN(limit)) {
|
2005-01-04 20:38:39 +03:00
|
|
|
limit = (struct RBignum *)rb_big_clone(vmax);
|
2007-09-01 16:02:36 +04:00
|
|
|
RBIGNUM_SET_SIGN(limit, 1);
|
2005-01-04 20:38:39 +03:00
|
|
|
}
|
|
|
|
limit = (struct RBignum *)rb_big_minus((VALUE)limit, INT2FIX(1));
|
|
|
|
if (FIXNUM_P((VALUE)limit)) {
|
|
|
|
if (FIX2LONG((VALUE)limit) == -1)
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
return DOUBLE2NUM(genrand_real());
|
2005-01-04 20:38:39 +03:00
|
|
|
return LONG2NUM(limited_rand(FIX2LONG((VALUE)limit)));
|
|
|
|
}
|
|
|
|
return limited_big_rand(limit);
|
2002-07-26 10:12:39 +04:00
|
|
|
}
|
2000-07-04 08:17:26 +04:00
|
|
|
case T_NIL:
|
|
|
|
max = 0;
|
|
|
|
break;
|
|
|
|
default:
|
2003-01-31 06:15:33 +03:00
|
|
|
vmax = rb_Integer(vmax);
|
2003-01-31 06:22:42 +03:00
|
|
|
if (TYPE(vmax) == T_BIGNUM) goto bignum;
|
2003-01-31 06:15:33 +03:00
|
|
|
case T_FIXNUM:
|
2004-03-15 05:27:29 +03:00
|
|
|
max = FIX2LONG(vmax);
|
2000-07-04 08:17:26 +04:00
|
|
|
break;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (max == 0) {
|
* include/ruby/ruby.h: introduce 2 macros:
RFLOAT_VALUE(v), DOUBLE2NUM(dbl).
Rename RFloat#value -> RFloat#double_value.
Do not touch RFloat#double_value directly.
* bignum.c, insns.def, marshal.c, math.c, numeric.c, object.c,
pack.c, parse.y, process.c, random.c, sprintf.c, string.c,
time.c: apply above changes.
* ext/dl/mkcallback.rb, ext/json/ext/generator/generator.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13913 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-11-13 19:00:53 +03:00
|
|
|
return DOUBLE2NUM(genrand_real());
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2002-07-26 10:12:39 +04:00
|
|
|
if (max < 0) max = -max;
|
2005-01-04 20:38:39 +03:00
|
|
|
val = limited_rand(max-1);
|
2002-04-25 17:57:01 +04:00
|
|
|
return LONG2NUM(val);
|
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
|
|
|
{
|
2006-08-31 12:24:36 +04:00
|
|
|
rand_init(random_seed());
|
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);
|
2005-01-04 20:38:39 +03:00
|
|
|
rb_global_variable(&saved_seed);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|