Class: Random
Constant Summary collapse
- DEFAULT =
rb_Random_DEFAULT
Class Method Summary collapse
-
.new_seed ⇒ Integer
Returns arbitrary value for seed.
-
.rand ⇒ Object
Alias of Random::DEFAULT.rand.
-
.srand(number = 0) ⇒ Object
Seeds the pseudorandom number generator to the value of number.
Instance Method Summary collapse
-
#==(prng2) ⇒ Boolean
Returns true if the generators' states equal.
-
#bytes(size) ⇒ String
Returns a random binary string.
-
#new([seed]) ⇒ Object
constructor
Creates new Mersenne Twister based pseudorandom number generator with seed.
-
#initialize_copy ⇒ Object
:nodoc:.
-
#left ⇒ Object
:nodoc:.
-
#marshal_dump ⇒ Object
:nodoc:.
-
#marshal_load ⇒ Object
:nodoc:.
-
#rand ⇒ Object
When the argument is an
Integer
or aBignum
, it returns a random integer greater than or equal to zero and less than the argument. -
#seed ⇒ Integer
Returns the seed of the generator.
-
#state ⇒ Object
:nodoc:.
Constructor Details
#new([seed]) ⇒ Object
Creates new Mersenne Twister based pseudorandom number generator with seed. When the argument seed is omitted, the generator is initialized with Random.new_seed.
The argument seed is used to ensure repeatable sequences of random numbers between different runs of the program.
prng = Random.new(1234)
[ prng.rand, prng.rand ] #=> [0.191519450378892, 0.622108771039832]
[ prng.integer(10), prng.integer(1000) ] #=> [4, 664]
prng = Random.new(1234)
[ prng.rand, prng.rand ] #=> [0.191519450378892, 0.622108771039832]
|
# File 'random.c'
static VALUE
random_init(int argc, VALUE *argv, VALUE obj)
{
VALUE vseed;
rb_random_t *rnd = get_rnd(obj);
if (argc == 0) {
vseed = random_seed();
}
|
Class Method Details
.new_seed ⇒ Integer
Returns arbitrary value for seed.
|
# File 'random.c'
static VALUE
random_seed(void)
{
unsigned int buf[DEFAULT_SEED_CNT];
fill_random_seed(buf);
return make_seed_value(buf);
}
|
.rand ⇒ Float .rand(limit) ⇒ Numeric
Alias of Random::DEFAULT.rand.
|
# File 'random.c'
static VALUE
random_s_rand(int argc, VALUE *argv, VALUE obj)
{
return random_rand(argc, argv, rb_Random_DEFAULT);
}
|
.srand(number = 0) ⇒ Object
Seeds the pseudorandom number generator to the value of number. If number is omitted, seeds the generator using a combination of the time, the process id, and a sequence number. (This is also the behavior if Kernel::rand
is called without previously calling srand
, 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 Kernel::rand
.
|
# File 'random.c'
static VALUE
rb_f_srand(int argc, VALUE *argv, VALUE obj)
{
VALUE seed, old;
rb_random_t *r = &default_rand;
rb_secure(4);
if (argc == 0) {
seed = random_seed();
}
|
Instance Method Details
#==(prng2) ⇒ Boolean
Returns true if the generators' states equal.
|
# File 'random.c'
static VALUE
random_equal(VALUE self, VALUE other)
{
rb_random_t *r1, *r2;
if (rb_obj_class(self) != rb_obj_class(other)) return Qfalse;
r1 = get_rnd(self);
r2 = get_rnd(other);
if (!RTEST(rb_funcall2(r1->seed, rb_intern("=="), 1, &r2->seed))) return Qfalse;
if (memcmp(r1->mt.state, r2->mt.state, sizeof(r1->mt.state))) return Qfalse;
if ((r1->mt.next - r1->mt.state) != (r2->mt.next - r2->mt.state)) return Qfalse;
if (r1->mt.left != r2->mt.left) return Qfalse;
return Qtrue;
}
|
#bytes(size) ⇒ String
Returns a random binary string. The argument size specified the length of the result string.
|
# File 'random.c'
static VALUE
random_bytes(VALUE obj, VALUE len)
{
return rb_random_bytes(obj, NUM2LONG(rb_to_int(len)));
}
|
#initialize_copy ⇒ Object
:nodoc:
|
# File 'random.c'
static VALUE
random_copy(VALUE obj, VALUE orig)
{
rb_random_t *rnd1 = get_rnd(obj);
rb_random_t *rnd2 = get_rnd(orig);
struct MT *mt = &rnd1->mt;
*rnd1 = *rnd2;
mt->next = mt->state + numberof(mt->state) - mt->left + 1;
return obj;
}
|
#left ⇒ Object
:nodoc:
|
# File 'random.c'
static VALUE
random_left(VALUE obj)
{
rb_random_t *rnd = get_rnd(obj);
return INT2FIX(rnd->mt.left);
}
|
#marshal_dump ⇒ Object
:nodoc:
|
# File 'random.c'
static VALUE
random_dump(VALUE obj)
{
rb_random_t *rnd = get_rnd(obj);
VALUE dump = rb_ary_new2(3);
rb_ary_push(dump, mt_state(&rnd->mt));
rb_ary_push(dump, INT2FIX(rnd->mt.left));
rb_ary_push(dump, rnd->seed);
return dump;
}
|
#marshal_load ⇒ Object
:nodoc:
|
# File 'random.c'
static VALUE
random_load(VALUE obj, VALUE dump)
{
rb_random_t *rnd = get_rnd(obj);
struct MT *mt = &rnd->mt;
VALUE state, left = INT2FIX(1), seed = INT2FIX(0);
VALUE *ary;
unsigned long x;
Check_Type(dump, T_ARRAY);
ary = RARRAY_PTR(dump);
switch (RARRAY_LEN(dump)) {
case 3:
seed = ary[2];
case 2:
left = ary[1];
case 1:
state = ary[0];
break;
default:
rb_raise(rb_eArgError, "wrong dump data");
}
|
#rand ⇒ Float #rand(limit) ⇒ Numeric
When the argument is an Integer
or a Bignum
, it returns a random integer greater than or equal to zero and less than the argument. Unlike Random.rand, when the argument is a negative integer or zero, it raises an ArgumentError.
When the argument is a Float
, it returns a random floating point number between 0.0 and max, including 0.0 and excluding max.
When the argument limit is a Range
, it returns a random number where range.member?(number) == true.
prng.rand(5..9) #=> one of [5, 6, 7, 8, 9]
prng.rand(5...9) #=> one of [5, 6, 7, 8]
prng.rand(5.0..9.0) #=> between 5.0 and 9.0, including 9.0
prng.rand(5.0...9.0) #=> between 5.0 and 9.0, excluding 9.0
begin
/end
of the range have to have subtract and add methods.
Otherwise, it raises an ArgumentError.
|
# File 'random.c'
static VALUE
random_rand(int argc, VALUE *argv, VALUE obj)
{
rb_random_t *rnd = get_rnd(obj);
VALUE vmax, v;
if (argc == 0) {
return rb_float_new(genrand_real(&rnd->mt));
}
|
#seed ⇒ Integer
Returns the seed of the generator.
|
# File 'random.c'
static VALUE
random_get_seed(VALUE obj)
{
return get_rnd(obj)->seed;
}
|
#state ⇒ Object
:nodoc:
|
# File 'random.c'
static VALUE
random_state(VALUE obj)
{
rb_random_t *rnd = get_rnd(obj);
return mt_state(&rnd->mt);
}
|