Sha256: 5f05a2ca85415c82366a3899451376d7d189759a0f91280321619b76db763781

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

#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

int n_bits(u_int64_t a)
{
    int i, x, /*xu,*/ xl, n=5;
    u_int64_t m;

    if (a==0) return 0;
    //if (a<0) a=-a;

    x  = 1<<n;
    //xu = 1<<(n+1);
    xl = 0;
    //printf("%3i, [%3i, %3i], %i\n", i, xu, xl, x);

    for (i=n; i>=0; i--) {
	m = ~((1<<(x-1))-1);
	if (m & a) {
	    xl = x;
	    x += 1<<(i-1);
	} else {
	    //xu = x;
	    x -= 1<<(i-1);
	}
	//printf("%3i, [%3i, %3i], %i, 0x%lx, 0x%lx\n", i, xu, xl, x, m, m&a);
    }
    return xl;
}

static u_int64_t
 random_seed()
{
    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() {
    rb_define_singleton_method(cNArray, "srand", nary_s_srand, -1);
    init_gen_rand(0);
}

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
numo-narray-0.9.1.2 ext/numo/narray/rand.c
numo-narray-0.9.1.1 ext/numo/narray/rand.c
numo-narray-0.9.1.0 ext/numo/narray/rand.c
numo-narray-0.9.0.9 ext/numo/narray/rand.c
numo-narray-0.9.0.8 ext/numo/narray/rand.c
numo-narray-0.9.0.7 ext/numo/narray/rand.c
numo-narray-0.9.0.6 ext/numo/narray/rand.c
numo-narray-0.9.0.5 ext/numo/narray/rand.c
numo-narray-0.9.0.4 ext/numo/narray/rand.c