Class: Numo::GSL::Rng

Inherits:
Object
  • Object
show all
Includes:
Ran
Defined in:
ext/numo/gsl/rng/gsl_rng.c

Defined Under Namespace

Classes: Borosh13, Cmrg, Coveyou, Default, Fishman18, Fishman20, Fishman2x, Gfsr4, Knuthran, Knuthran2, Knuthran2002, Lecuyer21, Minstd, Mrg, Mt19937, Mt199371998, Mt199371999, R250, Ran0, Ran1, Ran2, Ran3, Rand, Rand48, Random128Bsd, Random128Glibc2, Random128Libc5, Random256Bsd, Random256Glibc2, Random256Libc5, Random32Bsd, Random32Glibc2, Random32Libc5, Random64Bsd, Random64Glibc2, Random64Libc5, Random8Bsd, Random8Glibc2, Random8Libc5, RandomBsd, RandomGlibc2, RandomLibc5, Randu, Ranf, Ranlux, Ranlux389, Ranlxd1, Ranlxd2, Ranlxs0, Ranlxs1, Ranlxs2, Ranmar, Slatec, Taus, Taus113, Taus2, Transputer, Tt800, Uni, Uni32, Vax, Waterman14, Zuf

Instance Method Summary collapse

Methods included from Ran

#bernoulli, #beta, #binomial, #bivariate_gaussian, #cauchy, #chisq, #dir_2d, #dir_2d_trig_method, #dir_3d, #dirichlet, #exponential, #exppow, #fdist, #flat, #gamma, #gamma_knuth, #gaussian, #gaussian_ratio_method, #gaussian_tail, #gaussian_ziggurat, #geometric, #gumbel1, #gumbel2, #hypergeometric, #landau, #laplace, #levy, #logarithmic, #logistic, #lognormal, #multinomial, #pareto, #pascal, #poisson, #rayleigh, #rayleigh_tail, #tdist, #ugaussian, #ugaussian_ratio_method, #ugaussian_tail, #weibull

Instance Method Details

#cloneNumo::GSL::Rng

This function returns a pointer to a newly created generator which is an exact copy of the generator r.

Returns:



2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2584

static VALUE
rng_clone(VALUE self)
{
    gsl_rng *x, *w;

    TypedData_Get_Struct(self, gsl_rng, &rng_data_type, x);

    w = gsl_rng_clone(x);
    if (!w) {
        rb_raise(rb_eNoMemError,"fail to allocate struct");
    }
    return TypedData_Wrap_Struct(cRng, &rng_data_type, (void*)w);
}

#getInteger

This function returns a random integer from the generator r. The minimum and maximum values depend on the algorithm used, but all integers in the range [min,max] are equally likely. The values of min and max can be determined using the auxiliary functions gsl_rng_max (r) and gsl_rng_min (r).

Returns:

  • (Integer)


2312
2313
2314
2315
2316
2317
2318
2319
2320
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2312

static VALUE
rng_get(VALUE self)
{
    gsl_rng *w;

    TypedData_Get_Struct(self, gsl_rng, &rng_data_type, w);

    return ULONG2NUM(gsl_rng_get(w));
}

#maxInteger

gsl_rng_max returns the largest value that gsl_rng_get can return.

Returns:

  • (Integer)


2497
2498
2499
2500
2501
2502
2503
2504
2505
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2497

static VALUE
rng_max(VALUE self)
{
    gsl_rng *w;

    TypedData_Get_Struct(self, gsl_rng, &rng_data_type, w);

    return ULONG2NUM(gsl_rng_max(w));
}

#memcpy(src) ⇒ Numo::GSL::Rng

This function copies the random number generator src into the pre-existing generator dest, making dest into an exact copy of src. The two generators must be of the same type.

Parameters:

Returns:



2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2564

static VALUE
rng_memcpy(VALUE self, VALUE other)
{
    gsl_rng *w, *x;

    TypedData_Get_Struct(self, gsl_rng, &rng_data_type, w);
    TypedData_Get_Struct(other, gsl_rng, &rng_data_type, x);
    gsl_rng_memcpy(w,x);
    return self;
}

#minInteger

gsl_rng_min returns the smallest value that gsl_rng_get can return. Usually this value is zero. There are some generators with algorithms that cannot return zero, and for these generators the minimum value is 1.

Returns:

  • (Integer)


2518
2519
2520
2521
2522
2523
2524
2525
2526
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2518

static VALUE
rng_min(VALUE self)
{
    gsl_rng *w;

    TypedData_Get_Struct(self, gsl_rng, &rng_data_type, w);

    return ULONG2NUM(gsl_rng_min(w));
}

#nameString

This function returns a pointer to the name of the generator. For example,

printf (“r is a ‘%s’ generator\n”, gsl_rng_name (r));

would print something like r is a ‘taus’ generator.

Returns:

  • (String)


2478
2479
2480
2481
2482
2483
2484
2485
2486
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2478

static VALUE
rng_name(VALUE self)
{
    gsl_rng *w;

    TypedData_Get_Struct(self, gsl_rng, &rng_data_type, w);

    return rb_str_new_cstr(gsl_rng_name(w));
}

#set(s) ⇒ Qnil

This function initializes (or `seeds’) the random number generator. If the generator is seeded with the same value of s on two different runs, the same stream of random numbers will be generated by successive calls to the routines below. If different values of $s \geq 1$ s >= 1 are supplied, then the generated streams of random numbers should be completely different. If the seed s is zero then the standard seed from the original implementation is used instead. For example, the original Fortran source code for the ranlux generator used a seed of 314159265, and so choosing s equal to zero reproduces this when using gsl_rng_ranlux.

When using multiple seeds with the same generator, choose seed values greater than zero to avoid collisions with the default setting.

Note that the most generators only accept 32-bit seeds, with higher values being reduced modulo $2^32$ 2^32. For generators with smaller ranges the maximum seed value will typically be lower.

Parameters:

  • s (Integer)

Returns:

  • (Qnil)


2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2289

static VALUE
rng_set(VALUE self, VALUE v1)
{
    gsl_rng *w;

    TypedData_Get_Struct(self, gsl_rng, &rng_data_type, w);

    gsl_rng_set(w, NUM2ULONG(v1));
    return Qnil;
}

#sizeInteger

These functions return a pointer to the state of generator r and its size. You can use this information to access the state directly. For example, the following code will write the state of a generator to a stream,

void * state = gsl_rng_state (r); size_t n = gsl_rng_size (r); fwrite (state, n, 1, stream);

Returns:

  • (Integer)


2543
2544
2545
2546
2547
2548
2549
2550
2551
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2543

static VALUE
rng_size(VALUE self)
{
    gsl_rng *w;

    TypedData_Get_Struct(self, gsl_rng, &rng_data_type, w);

    return SIZET2NUM(gsl_rng_size(w));
}

#uniform([shape]) ⇒ Float or DFloat

This function returns a double precision floating point number uniformly distributed in the range [0,1). The range includes 0.0 but excludes 1.0. The value is typically obtained by dividing the result of gsl_rng_get(r) by gsl_rng_max(r) + 1.0 in double precision. Some generators compute this ratio internally so that they can provide floating point numbers with more than 32 bits of randomness (the maximum number of bits that can be portably represented in a single unsigned long int).

Parameters:

  • shape (Array or Integer)

    (optional) shape for result NArray

Returns:

  • (Float or DFloat)

    returns random number



2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2338

static VALUE
rng_uniform(int argc, VALUE *argv, VALUE self)
{
    VALUE vshape, vna;
    size_t i, size;
    double *ptr;
    gsl_rng *r;

    TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);

    if (rb_scan_args(argc, argv, "01", &vshape) == 0) {
        return rb_float_new(gsl_rng_uniform(r));
    } else {
        vna = create_new_narray(cDF,vshape);
        ptr = (double*)na_get_pointer_for_write(vna);
        size = RNARRAY_SIZE(vna);
        for (i=0; i<size; i++) {
            ptr[i] = gsl_rng_uniform(r);
        }
        return vna;
    }
}

#uniform_int(n, [shape]) ⇒ Integer or UInt32/UInt64

This function returns a random integer from 0 to n-1 inclusive by scaling down and/or discarding samples from the generator r. All integers in the range [0,n-1] are produced with equal probability. For generators with a non-zero minimum value an offset is applied so that zero is returned with the correct probability.

Note that this function is designed for sampling from ranges smaller than the range of the underlying generator. The parameter n must be less than or equal to the range of the generator r. If n is larger than the range of the generator then the function calls the error handler with an error code of GSL_EINVAL and returns zero.

In particular, this function is not intended for generating the full range of unsigned integer values $[0,2^32-1]$ [0,2^32-1]. Instead choose a generator with the maximal integer range and zero minimum value, such as gsl_rng_ranlxd1, gsl_rng_mt19937 or gsl_rng_taus, and sample it directly using gsl_rng_get. The range of each generator can be found using the auxiliary functions described in the next section.

Parameters:

  • n (Integer)
  • shape (Array or Integer)

    (optional) shape for result NArray

Returns:

  • (Integer or UInt32/UInt64)

    returns random number



2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2427

static VALUE
rng_uniform_int(int argc, VALUE *argv, VALUE self)
{
    VALUE vshape, vna, vn;
    size_t i, size;
    int nargs;
    unsigned long n;
    u_int32_t *p32;
    u_int64_t *p64;
    gsl_rng *r;

    TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);

    nargs = rb_scan_args(argc, argv, "11", &vn, &vshape);
    n = NUM2ULONG(vn);
    if (nargs == 1) {
        return ULONG2NUM(gsl_rng_uniform_int(r, n));
    } else {
        if (n > 4294967295ul) {
            vna = create_new_narray(numo_cUInt64,vshape);
            p64 = (u_int64_t*)na_get_pointer_for_write(vna);
            size = RNARRAY_SIZE(vna);
            for (i=0; i<size; i++) {
                p64[i] = (u_int64_t)gsl_rng_uniform_int(r, n);
            }
        } else {
            vna = create_new_narray(numo_cUInt32,vshape);
            p32 = (u_int32_t*)na_get_pointer_for_write(vna);
            size = RNARRAY_SIZE(vna);
            for (i=0; i<size; i++) {
                p32[i] = (u_int32_t)gsl_rng_uniform_int(r, n);
            }
        }
        return vna;
    }
}

#uniform_pos([shape]) ⇒ Float or DFloat

This function returns a positive double precision floating point number uniformly distributed in the range (0,1), excluding both 0.0 and 1.0. The number is obtained by sampling the generator with the algorithm of gsl_rng_uniform until a non-zero value is obtained. You can use this function if you need to avoid a singularity at 0.0.

Parameters:

  • shape (Array or Integer)

    (optional) shape for result NArray

Returns:

  • (Float or DFloat)

    returns random number



2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
# File 'ext/numo/gsl/rng/gsl_rng.c', line 2374

static VALUE
rng_uniform_pos(int argc, VALUE *argv, VALUE self)
{
    VALUE vshape, vna;
    size_t i, size;
    double *ptr;
    gsl_rng *r;

    TypedData_Get_Struct(self, gsl_rng, &rng_data_type, r);

    if (rb_scan_args(argc, argv, "01", &vshape) == 0) {
        return rb_float_new(gsl_rng_uniform_pos(r));
    } else {
        vna = create_new_narray(cDF,vshape);
        ptr = (double*)na_get_pointer_for_write(vna);
        size = RNARRAY_SIZE(vna);
        for (i=0; i<size; i++) {
            ptr[i] = gsl_rng_uniform_pos(r);
        }
        return vna;
    }
}