Class: Random

Inherits:
Object show all
Defined in:
random.c

Constant Summary collapse

DEFAULT =
rb_Random_DEFAULT

Class Method Summary collapse

Instance Method Summary collapse

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_seedInteger

Returns arbitrary value for seed.

Returns:



# File 'random.c'

static VALUE
random_seed(void)
{
    unsigned int buf[DEFAULT_SEED_CNT];
    fill_random_seed(buf);
    return make_seed_value(buf);
}

.randFloat .rand(limit) ⇒ Numeric

Alias of Random::DEFAULT.rand.

Overloads:



# 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.

Returns:

  • (Boolean)


# 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.

Returns:



# File 'random.c'

static VALUE
random_bytes(VALUE obj, VALUE len)
{
    return rb_random_bytes(obj, NUM2LONG(rb_to_int(len)));
}

#initialize_copyObject

: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;
}

#leftObject

:nodoc:



# File 'random.c'

static VALUE
random_left(VALUE obj)
{
    rb_random_t *rnd = get_rnd(obj);
    return INT2FIX(rnd->mt.left);
}

#marshal_dumpObject

: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_loadObject

: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");
}

#randFloat #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.

Overloads:



# 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));
}

#seedInteger

Returns the seed of the generator.

Returns:



# File 'random.c'

static VALUE
random_get_seed(VALUE obj)
{
    return get_rnd(obj)->seed;
}

#stateObject

:nodoc:



# File 'random.c'

static VALUE
random_state(VALUE obj)
{
    rb_random_t *rnd = get_rnd(obj);
    return mt_state(&rnd->mt);
}