Class: Random

Inherits:
Object
  • Object
show all
Defined in:
mrbgems/mruby-random/src/random.c

Instance Method Summary collapse

Constructor Details

#initializeObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'mrbgems/mruby-random/src/random.c', line 157

static mrb_value
random_m_init(mrb_state *mrb, mrb_value self)
{
  mrb_value seed;
  rand_state *t;

  seed = get_opt(mrb);
  /* avoid memory leaks */
  t = random_ptr(self);
  if (mrb_nil_p(seed)) {
    rand_init(t);
  }
  else {
    rand_seed(t, (uint32_t)mrb_fixnum(seed));
  }

  return self;
}

Instance Method Details

#randObject



176
177
178
179
180
181
182
183
184
# File 'mrbgems/mruby-random/src/random.c', line 176

static mrb_value
random_m_rand(mrb_state *mrb, mrb_value self)
{
  mrb_value max;
  rand_state *t = random_ptr(self);

  max = get_opt(mrb);
  return random_rand(mrb, t, max);
}

#srandObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'mrbgems/mruby-random/src/random.c', line 186

static mrb_value
random_m_srand(mrb_state *mrb, mrb_value self)
{
  uint32_t seed;
  uint32_t old_seed;
  mrb_value sv;
  rand_state *t = random_ptr(self);

  sv = get_opt(mrb);
  if (mrb_nil_p(sv)) {
    seed = (uint32_t)time(NULL) + rand_uint32(t);
  }
  else {
    seed = (uint32_t)mrb_fixnum(sv);
  }
  old_seed = rand_seed(t, seed);

  return mrb_fixnum_value((mrb_int)old_seed);
}