Extended maintenance of Ruby 1.9.3 ended on February 23, 2015. Read more

In Files

  • random.c

Random

Constants

DEFAULT

Public Class Methods

new([seed]) → prng click to toggle source

Creates new Mersenne Twister based pseudorandom number generator with seed. When the argument seed is omitted, the generator is initialized with ::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]
 
               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();
    }
    else {
        rb_scan_args(argc, argv, "01", &vseed);
    }
    rnd->seed = rand_init(&rnd->mt, vseed);
    return obj;
}
            
new_seed → integer click to toggle source

Returns arbitrary value for seed.

 
               static VALUE
random_seed(void)
{
    unsigned int buf[DEFAULT_SEED_CNT];
    fill_random_seed(buf);
    return make_seed_value(buf);
}
            
rand → float click to toggle source
rand(limit) → number

Alias of Random::DEFAULT.rand.

 
               static VALUE
random_s_rand(int argc, VALUE *argv, VALUE obj)
{
    rand_start(&default_rand);
    return random_rand(argc, argv, rb_Random_DEFAULT);
}
            
srand(number=0) → old_seed click to toggle source

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.

 
               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();
    }
    else {
        rb_scan_args(argc, argv, "01", &seed);
    }
    old = r->seed;
    r->seed = rand_init(&r->mt, seed);

    return old;
}
            

Public Instance Methods

prng1 == prng2 → true or false click to toggle source

Returns true if the generators’ states equal.

 
               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) → a_string click to toggle source

Returns a random binary string. The argument size specified the length of the result string.

 
               static VALUE
random_bytes(VALUE obj, VALUE len)
{
    return rb_random_bytes(obj, NUM2LONG(rb_to_int(len)));
}
            
rand → float click to toggle source
rand(limit) → number

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

 
               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));
    }
    else if (argc != 1) {
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..1)", argc);
    }
    vmax = argv[0];
    if (NIL_P(vmax)) {
        v = Qnil;
    }
    else if (TYPE(vmax) != T_FLOAT && (v = rb_check_to_integer(vmax, "to_int"), !NIL_P(v))) {
        v = rand_int(&rnd->mt, v, 1);
    }
    else if (v = rb_check_to_float(vmax), !NIL_P(v)) {
        double max = float_value(v);
        if (max > 0.0)
            v = rb_float_new(max * genrand_real(&rnd->mt));
        else
            v = Qnil;
    }
    else if ((v = rand_range(&rnd->mt, vmax)) != Qfalse) {
        /* nothing to do */
    }
    else {
        v = Qnil;
        (void)NUM2LONG(vmax);
    }
    if (NIL_P(v)) {
        VALUE mesg = rb_str_new_cstr("invalid argument - ");
        rb_str_append(mesg, rb_obj_as_string(argv[0]));
        rb_exc_raise(rb_exc_new3(rb_eArgError, mesg));
    }

    return v;
}
            
seed → integer click to toggle source

Returns the seed of the generator.

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