forked from ruby-numo/numo-narray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrand.c
More file actions
45 lines (38 loc) · 773 Bytes
/
rand.c
File metadata and controls
45 lines (38 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "ruby.h"
#include "numo/narray.h"
#include "SFMT.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <time.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
static u_int64_t
random_seed(void)
{
static int n = 0;
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec ^ tv.tv_usec ^ getpid() ^ n++;
}
static VALUE
nary_s_srand(int argc, VALUE *argv, VALUE obj)
{
VALUE vseed;
u_int64_t seed;
//rb_secure(4);
if (rb_scan_args(argc, argv, "01", &vseed) == 0) {
seed = random_seed();
}
else {
seed = NUM2UINT64(vseed);
}
init_gen_rand(seed);
return Qnil;
}
void
Init_nary_rand(void) {
rb_define_singleton_method(cNArray, "srand", nary_s_srand, -1);
init_gen_rand(0);
}