Class: ISAAC

Inherits:
Object show all
Defined in:
ext/redshift/util/isaac/isaac.c

Direct Known Subclasses

ISAACGenerator

Instance Method Summary collapse

Instance Method Details

#marshal_dumpObject

:nodoc:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'ext/redshift/util/isaac/isaac.c', line 77

static VALUE
ISAAC_marshal_dump(VALUE self)
{
    randctx *ctx;
    int i;
    int ary_size = sizeof(randctx)/sizeof(ub4);
    VALUE ary;

    Data_Get_Struct(self, randctx, ctx);
    
    ary = rb_ary_new2(ary_size);
    for (i = 0; i < ary_size; i++) {
        rb_ary_push(ary, UINT2NUM(((ub4 *)ctx)[i]));
    }
    
    return ary;
}

#marshal_load(ary) ⇒ Object

:nodoc:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'ext/redshift/util/isaac/isaac.c', line 96

static VALUE
ISAAC_marshal_load(VALUE self, VALUE ary)
{
    randctx *ctx;
    int i;
    int ary_size = sizeof(randctx)/sizeof(ub4);

    Data_Get_Struct(self, randctx, ctx);

    if (RARRAY_LEN(ary) != ary_size)
        rb_raise(rb_eArgError, "bad length in loaded ISAAC data");

    for (i = 0; i < ary_size; i++) {
        ((ub4 *)ctx)[i] = NUM2UINT(RARRAY_PTR(ary)[i]);
    }
    
    return self;
}

#randObject

Return a random float in the range 0..1.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/redshift/util/isaac/isaac.c', line 61

static VALUE
ISAAC_rand(VALUE self)
{
    randctx *ctx;

    Data_Get_Struct(self, randctx, ctx);

    if (!ctx->randcnt--) {
        rs_isaac_rand(ctx);
        ctx->randcnt=RANDSIZ-1;
    }
    
    return rb_float_new(ctx->randrsl[ctx->randcnt] / 4294967295.0);
}

#rand32Object

Return a random integer in the range 0..2**32-1.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'ext/redshift/util/isaac/isaac.c', line 43

static VALUE
ISAAC_rand32(VALUE self)
{
    randctx *ctx;

    Data_Get_Struct(self, randctx, ctx);

    if (!ctx->randcnt--) {
        rs_isaac_rand(ctx);
        ctx->randcnt=RANDSIZ-1;
    }
    
    return UINT2NUM(ctx->randrsl[ctx->randcnt]);
}

#srand(ary) ⇒ Object

Seed the generator with an array of up to ISAAC::RANDSIZ integers in the range 0..2**32-1. More entries are ignored. Missing entries are treated as 0. Returns nil.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'ext/redshift/util/isaac/isaac.c', line 21

static VALUE
ISAAC_srand(VALUE self, VALUE ary)
{
    int i;
    randctx *ctx;

    Check_Type(ary, T_ARRAY);
    
    Data_Get_Struct(self, randctx, ctx);
    
    MEMZERO(ctx, randctx, 1);
    for (i=min(RANDSIZ, RARRAY_LEN(ary))-1; i>=0; i--) {
        ctx->randrsl[i] = NUM2UINT(RARRAY_PTR(ary)[i]);
    }
    rs_isaac_init(ctx, 1);

    return Qnil;
}