tuplex 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/COPYING +22 -0
- data/README.md +16 -0
- data/ext/isaac/extconf.rb +2 -0
- data/ext/isaac/isaac.c +247 -0
- data/ext/isaac/rand.c +139 -0
- data/ext/isaac/rand.h +60 -0
- data/ext/isaac/rand4.c +131 -0
- data/ext/isaac/rand4.h +49 -0
- data/lib/tuplex/version.rb +3 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b58ffe6b6580580163d4fe58df687ad5ea20e6de
|
4
|
+
data.tar.gz: 5289b5307ea0788d2ad79ba50f8ee9b957f5b714
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 652c10a64977a7b1a61b908b438615e47bd6f74c7db78e141bb6cb84552d51357dfb0078080cd8146fbbb048a95f7ff3ccbe6125d8f96761df2df4a9c550ff6d
|
7
|
+
data.tar.gz: 7edc4d53d20e441c2feb42957e46e91de37e30aea649024743e59385e4b64df3723f1211c98a7e4b423d41aa73c9335401d72ad2727a5be4be293395ab799a40
|
data/COPYING
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013-2014, Joel VanderWerf
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
|
13
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
14
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
15
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
16
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
17
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
18
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
19
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
20
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
21
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
22
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Tuplex
|
2
|
+
==
|
3
|
+
|
4
|
+
Constructs index keys for tuples.
|
5
|
+
|
6
|
+
Contact
|
7
|
+
=======
|
8
|
+
|
9
|
+
Joel VanderWerf, vjoel@users.sourceforge.net, [@JoelVanderWerf](https://twitter.com/JoelVanderWerf).
|
10
|
+
|
11
|
+
License and Copyright
|
12
|
+
========
|
13
|
+
|
14
|
+
Copyright (c) 2014, Joel VanderWerf
|
15
|
+
|
16
|
+
License for this project is BSD. See the COPYING file for the standard BSD license. The supporting gems developed for this project are similarly licensed.
|
data/ext/isaac/isaac.c
ADDED
@@ -0,0 +1,247 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
#include "rand.h"
|
3
|
+
#include "rand4.h"
|
4
|
+
|
5
|
+
#ifndef min
|
6
|
+
# define min(a,b) (((a)<(b)) ? (a) : (b))
|
7
|
+
#endif /* min */
|
8
|
+
|
9
|
+
static VALUE
|
10
|
+
ISAAC_s_allocate(VALUE klass)
|
11
|
+
{
|
12
|
+
randctx *ctx;
|
13
|
+
|
14
|
+
return Data_Make_Struct(klass, randctx, NULL, NULL, ctx);
|
15
|
+
}
|
16
|
+
|
17
|
+
static VALUE
|
18
|
+
ISAAC4_s_allocate(VALUE klass)
|
19
|
+
{
|
20
|
+
randctx4 *ctx;
|
21
|
+
|
22
|
+
return Data_Make_Struct(klass, randctx4, NULL, NULL, ctx);
|
23
|
+
}
|
24
|
+
|
25
|
+
/*
|
26
|
+
* Seed the generator with an array of up to ISAAC::RANDSIZ integers in the
|
27
|
+
* range 0..2**32-1. More entries are ignored. Missing entries are treated
|
28
|
+
* as 0. Returns +nil+.
|
29
|
+
*/
|
30
|
+
static VALUE
|
31
|
+
ISAAC_srand(VALUE self, VALUE ary)
|
32
|
+
{
|
33
|
+
int i;
|
34
|
+
randctx *ctx;
|
35
|
+
|
36
|
+
Check_Type(ary, T_ARRAY);
|
37
|
+
|
38
|
+
Data_Get_Struct(self, randctx, ctx);
|
39
|
+
|
40
|
+
MEMZERO(ctx, randctx, 1);
|
41
|
+
for (i=min(RANDSIZ, RARRAY_LEN(ary))-1; i>=0; i--) {
|
42
|
+
ctx->randrsl[i] = NUM2UINT(RARRAY_PTR(ary)[i]);
|
43
|
+
}
|
44
|
+
isaac_init(ctx, 1);
|
45
|
+
|
46
|
+
return Qnil;
|
47
|
+
}
|
48
|
+
|
49
|
+
/*
|
50
|
+
* Seed the generator with an array of up to ISAAC::RANDSIZ integers in the
|
51
|
+
* range 0..2**32-1. More entries are ignored. Missing entries are treated
|
52
|
+
* as 0. Returns +nil+.
|
53
|
+
*/
|
54
|
+
static VALUE
|
55
|
+
ISAAC4_srand(VALUE self, VALUE ary)
|
56
|
+
{
|
57
|
+
int i;
|
58
|
+
randctx4 *ctx;
|
59
|
+
|
60
|
+
Check_Type(ary, T_ARRAY);
|
61
|
+
|
62
|
+
Data_Get_Struct(self, randctx4, ctx);
|
63
|
+
|
64
|
+
MEMZERO(ctx, randctx4, 1);
|
65
|
+
for (i=min(RANDSIZ4, RARRAY_LEN(ary))-1; i>=0; i--) {
|
66
|
+
ctx->randrsl[i] = NUM2UINT(RARRAY_PTR(ary)[i]);
|
67
|
+
}
|
68
|
+
isaac_init4(ctx, 1);
|
69
|
+
|
70
|
+
return Qnil;
|
71
|
+
}
|
72
|
+
|
73
|
+
/*
|
74
|
+
* Return a random integer in the range 0..2**32-1.
|
75
|
+
*/
|
76
|
+
static VALUE
|
77
|
+
ISAAC_rand32(VALUE self)
|
78
|
+
{
|
79
|
+
randctx *ctx;
|
80
|
+
|
81
|
+
Data_Get_Struct(self, randctx, ctx);
|
82
|
+
|
83
|
+
if (!ctx->randcnt--) {
|
84
|
+
isaac_rand(ctx);
|
85
|
+
ctx->randcnt=RANDSIZ-1;
|
86
|
+
}
|
87
|
+
|
88
|
+
return UINT2NUM(ctx->randrsl[ctx->randcnt]);
|
89
|
+
}
|
90
|
+
|
91
|
+
/*
|
92
|
+
* Return a random integer in the range 0..2**32-1.
|
93
|
+
*/
|
94
|
+
static VALUE
|
95
|
+
ISAAC4_rand32(VALUE self)
|
96
|
+
{
|
97
|
+
randctx4 *ctx;
|
98
|
+
|
99
|
+
Data_Get_Struct(self, randctx4, ctx);
|
100
|
+
|
101
|
+
if (!ctx->randcnt--) {
|
102
|
+
isaac_rand4(ctx);
|
103
|
+
ctx->randcnt=RANDSIZ4-1;
|
104
|
+
}
|
105
|
+
|
106
|
+
return UINT2NUM(ctx->randrsl[ctx->randcnt]);
|
107
|
+
}
|
108
|
+
|
109
|
+
/*
|
110
|
+
* Return a random float in the range 0..1.
|
111
|
+
*/
|
112
|
+
static VALUE
|
113
|
+
ISAAC_rand(VALUE self)
|
114
|
+
{
|
115
|
+
randctx *ctx;
|
116
|
+
|
117
|
+
Data_Get_Struct(self, randctx, ctx);
|
118
|
+
|
119
|
+
if (!ctx->randcnt--) {
|
120
|
+
isaac_rand(ctx);
|
121
|
+
ctx->randcnt=RANDSIZ-1;
|
122
|
+
}
|
123
|
+
|
124
|
+
return rb_float_new(ctx->randrsl[ctx->randcnt] / 4294967295.0);
|
125
|
+
}
|
126
|
+
|
127
|
+
/*
|
128
|
+
* Return a random float in the range 0..1.
|
129
|
+
*/
|
130
|
+
static VALUE
|
131
|
+
ISAAC4_rand(VALUE self)
|
132
|
+
{
|
133
|
+
randctx4 *ctx;
|
134
|
+
|
135
|
+
Data_Get_Struct(self, randctx4, ctx);
|
136
|
+
|
137
|
+
if (!ctx->randcnt--) {
|
138
|
+
isaac_rand4(ctx);
|
139
|
+
ctx->randcnt=RANDSIZ4-1;
|
140
|
+
}
|
141
|
+
|
142
|
+
return rb_float_new(ctx->randrsl[ctx->randcnt] / 4294967295.0);
|
143
|
+
}
|
144
|
+
|
145
|
+
static VALUE
|
146
|
+
ISAAC_marshal_dump(VALUE self)
|
147
|
+
{
|
148
|
+
randctx *ctx;
|
149
|
+
int i;
|
150
|
+
int ary_size = sizeof(randctx)/sizeof(ub4);
|
151
|
+
VALUE ary;
|
152
|
+
|
153
|
+
Data_Get_Struct(self, randctx, ctx);
|
154
|
+
|
155
|
+
ary = rb_ary_new2(ary_size);
|
156
|
+
for (i = 0; i < ary_size; i++) {
|
157
|
+
rb_ary_push(ary, UINT2NUM(((ub4 *)ctx)[i]));
|
158
|
+
}
|
159
|
+
|
160
|
+
return ary;
|
161
|
+
}
|
162
|
+
|
163
|
+
static VALUE
|
164
|
+
ISAAC4_marshal_dump(VALUE self)
|
165
|
+
{
|
166
|
+
randctx4 *ctx;
|
167
|
+
int i;
|
168
|
+
int ary_size = sizeof(randctx4)/sizeof(ub4);
|
169
|
+
VALUE ary;
|
170
|
+
|
171
|
+
Data_Get_Struct(self, randctx4, ctx);
|
172
|
+
|
173
|
+
ary = rb_ary_new2(ary_size);
|
174
|
+
for (i = 0; i < ary_size; i++) {
|
175
|
+
rb_ary_push(ary, UINT2NUM(((ub4 *)ctx)[i]));
|
176
|
+
}
|
177
|
+
|
178
|
+
return ary;
|
179
|
+
}
|
180
|
+
|
181
|
+
static VALUE
|
182
|
+
ISAAC_marshal_load(VALUE self, VALUE ary)
|
183
|
+
{
|
184
|
+
randctx *ctx;
|
185
|
+
int i;
|
186
|
+
int ary_size = sizeof(randctx)/sizeof(ub4);
|
187
|
+
|
188
|
+
Data_Get_Struct(self, randctx, ctx);
|
189
|
+
|
190
|
+
if (RARRAY_LEN(ary) != ary_size)
|
191
|
+
rb_raise(rb_eArgError, "bad length in loaded ISAAC data");
|
192
|
+
|
193
|
+
for (i = 0; i < ary_size; i++) {
|
194
|
+
((ub4 *)ctx)[i] = NUM2UINT(RARRAY_PTR(ary)[i]);
|
195
|
+
}
|
196
|
+
|
197
|
+
return self;
|
198
|
+
}
|
199
|
+
|
200
|
+
static VALUE
|
201
|
+
ISAAC4_marshal_load(VALUE self, VALUE ary)
|
202
|
+
{
|
203
|
+
randctx4 *ctx;
|
204
|
+
int i;
|
205
|
+
int ary_size = sizeof(randctx4)/sizeof(ub4);
|
206
|
+
|
207
|
+
Data_Get_Struct(self, randctx4, ctx);
|
208
|
+
|
209
|
+
if (RARRAY_LEN(ary) != ary_size)
|
210
|
+
rb_raise(rb_eArgError, "bad length in loaded ISAAC4 data");
|
211
|
+
|
212
|
+
for (i = 0; i < ary_size; i++) {
|
213
|
+
((ub4 *)ctx)[i] = NUM2UINT(RARRAY_PTR(ary)[i]);
|
214
|
+
}
|
215
|
+
|
216
|
+
return self;
|
217
|
+
}
|
218
|
+
|
219
|
+
void
|
220
|
+
Init_isaac()
|
221
|
+
{
|
222
|
+
VALUE ISAAC;
|
223
|
+
VALUE ISAAC4;
|
224
|
+
VALUE mPRNG;
|
225
|
+
|
226
|
+
mPRNG = rb_define_module("PRNG");
|
227
|
+
ISAAC = rb_define_class_under(mPRNG, "ISAAC", rb_cObject);
|
228
|
+
ISAAC4 = rb_define_class_under(mPRNG, "ISAAC4", rb_cObject);
|
229
|
+
rb_define_const(ISAAC, "VERSION", rb_str_new_cstr("0.1.2"));
|
230
|
+
|
231
|
+
rb_define_alloc_func(ISAAC, ISAAC_s_allocate);
|
232
|
+
rb_define_method(ISAAC, "srand", ISAAC_srand, 1);
|
233
|
+
rb_define_method(ISAAC, "rand32", ISAAC_rand32, 0);
|
234
|
+
rb_define_method(ISAAC, "rand", ISAAC_rand, 0);
|
235
|
+
rb_define_method(ISAAC, "marshal_dump", ISAAC_marshal_dump, 0);
|
236
|
+
rb_define_method(ISAAC, "marshal_load", ISAAC_marshal_load, 1);
|
237
|
+
|
238
|
+
rb_define_alloc_func(ISAAC4, ISAAC4_s_allocate);
|
239
|
+
rb_define_method(ISAAC4, "srand", ISAAC4_srand, 1);
|
240
|
+
rb_define_method(ISAAC4, "rand32", ISAAC4_rand32, 0);
|
241
|
+
rb_define_method(ISAAC4, "rand", ISAAC4_rand, 0);
|
242
|
+
rb_define_method(ISAAC4, "marshal_dump", ISAAC4_marshal_dump, 0);
|
243
|
+
rb_define_method(ISAAC4, "marshal_load", ISAAC4_marshal_load, 1);
|
244
|
+
|
245
|
+
rb_const_set(ISAAC, rb_intern("RANDSIZ"), UINT2NUM(RANDSIZ));
|
246
|
+
rb_const_set(ISAAC4, rb_intern("RANDSIZ"), UINT2NUM(RANDSIZ4));
|
247
|
+
}
|
data/ext/isaac/rand.c
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
/*
|
2
|
+
------------------------------------------------------------------------------
|
3
|
+
rand.c: By Bob Jenkins. My random number generator, ISAAC. Public Domain.
|
4
|
+
MODIFIED:
|
5
|
+
960327: Creation (addition of randinit, really)
|
6
|
+
970719: use context, not global variables, for internal state
|
7
|
+
980324: added main (ifdef'ed out), also rearranged randinit()
|
8
|
+
010626: Note that this is public domain
|
9
|
+
|
10
|
+
ADAPTED Aug2004 for use in Ruby by Joel VanderWerf
|
11
|
+
ADAPTED Jan2010 for 64 bit systems (same algorithm and results as 32 bit)
|
12
|
+
------------------------------------------------------------------------------
|
13
|
+
*/
|
14
|
+
#ifndef RAND
|
15
|
+
#include "rand.h"
|
16
|
+
#endif
|
17
|
+
|
18
|
+
|
19
|
+
#define ind(mm,x) ((mm)[(x>>2)&(RANDSIZ-1)])
|
20
|
+
#define rngstep(mix,a,b,mm,m,m2,r,x) \
|
21
|
+
{ \
|
22
|
+
x = *m; \
|
23
|
+
a = ((a^(mix)) + *(m2++)); \
|
24
|
+
*(m++) = y = (ind(mm,x) + a + b); \
|
25
|
+
*(r++) = b = (ind(mm,y>>RANDSIZL) + x); \
|
26
|
+
}
|
27
|
+
|
28
|
+
void isaac_rand(ctx)
|
29
|
+
randctx *ctx;
|
30
|
+
{
|
31
|
+
register ub4 a,b,x,y,*m,*mm,*m2,*r,*mend;
|
32
|
+
mm=ctx->randmem; r=ctx->randrsl;
|
33
|
+
a = ctx->randa; b = (ctx->randb + (++ctx->randc));
|
34
|
+
for (m = mm, mend = m2 = m+(RANDSIZ/2); m<mend; )
|
35
|
+
{
|
36
|
+
rngstep( a<<13, a, b, mm, m, m2, r, x);
|
37
|
+
rngstep( a>>6 , a, b, mm, m, m2, r, x);
|
38
|
+
rngstep( a<<2 , a, b, mm, m, m2, r, x);
|
39
|
+
rngstep( a>>16, a, b, mm, m, m2, r, x);
|
40
|
+
}
|
41
|
+
for (m2 = mm; m2<mend; )
|
42
|
+
{
|
43
|
+
rngstep( a<<13, a, b, mm, m, m2, r, x);
|
44
|
+
rngstep( a>>6 , a, b, mm, m, m2, r, x);
|
45
|
+
rngstep( a<<2 , a, b, mm, m, m2, r, x);
|
46
|
+
rngstep( a>>16, a, b, mm, m, m2, r, x);
|
47
|
+
}
|
48
|
+
ctx->randb = b; ctx->randa = a;
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
#define mix(a,b,c,d,e,f,g,h) \
|
53
|
+
{ \
|
54
|
+
a^=b<<11; d+=a; b+=c; \
|
55
|
+
b^=c>>2; e+=b; c+=d; \
|
56
|
+
c^=d<<8; f+=c; d+=e; \
|
57
|
+
d^=e>>16; g+=d; e+=f; \
|
58
|
+
e^=f<<10; h+=e; f+=g; \
|
59
|
+
f^=g>>4; a+=f; g+=h; \
|
60
|
+
g^=h<<8; b+=g; h+=a; \
|
61
|
+
h^=a>>9; c+=h; a+=b; \
|
62
|
+
}
|
63
|
+
|
64
|
+
/* if (flag==TRUE), then use the contents of randrsl[] to initialize mm[]. */
|
65
|
+
void isaac_init(ctx, flag)
|
66
|
+
randctx *ctx;
|
67
|
+
int flag;
|
68
|
+
{
|
69
|
+
int i;
|
70
|
+
ub4 a,b,c,d,e,f,g,h;
|
71
|
+
ub4 *m,*r;
|
72
|
+
ctx->randa = ctx->randb = ctx->randc = 0;
|
73
|
+
m=ctx->randmem;
|
74
|
+
r=ctx->randrsl;
|
75
|
+
a=b=c=d=e=f=g=h=0x9e3779b9; /* the golden ratio */
|
76
|
+
|
77
|
+
for (i=0; i<4; ++i) /* scramble it */
|
78
|
+
{
|
79
|
+
mix(a,b,c,d,e,f,g,h);
|
80
|
+
}
|
81
|
+
|
82
|
+
if (flag)
|
83
|
+
{
|
84
|
+
/* initialize using the contents of r[] as the seed */
|
85
|
+
for (i=0; i<RANDSIZ; i+=8)
|
86
|
+
{
|
87
|
+
a+=r[i ]; b+=r[i+1]; c+=r[i+2]; d+=r[i+3];
|
88
|
+
e+=r[i+4]; f+=r[i+5]; g+=r[i+6]; h+=r[i+7];
|
89
|
+
mix(a,b,c,d,e,f,g,h);
|
90
|
+
m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
|
91
|
+
m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
|
92
|
+
}
|
93
|
+
/* do a second pass to make all of the seed affect all of m */
|
94
|
+
for (i=0; i<RANDSIZ; i+=8)
|
95
|
+
{
|
96
|
+
a+=m[i ]; b+=m[i+1]; c+=m[i+2]; d+=m[i+3];
|
97
|
+
e+=m[i+4]; f+=m[i+5]; g+=m[i+6]; h+=m[i+7];
|
98
|
+
mix(a,b,c,d,e,f,g,h);
|
99
|
+
m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
|
100
|
+
m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
else
|
104
|
+
{
|
105
|
+
/* fill in mm[] with messy stuff */
|
106
|
+
for (i=0; i<RANDSIZ; i+=8)
|
107
|
+
{
|
108
|
+
mix(a,b,c,d,e,f,g,h);
|
109
|
+
m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
|
110
|
+
m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
isaac_rand(ctx); /* fill in the first set of results */
|
115
|
+
ctx->randcnt=RANDSIZ; /* prepare to use the first set of results */
|
116
|
+
}
|
117
|
+
|
118
|
+
|
119
|
+
#ifdef NEVER
|
120
|
+
#include <stdio.h>
|
121
|
+
|
122
|
+
int main()
|
123
|
+
{
|
124
|
+
ub4 i,j;
|
125
|
+
randctx ctx;
|
126
|
+
ctx.randa=ctx.randb=ctx.randc=(ub4)0;
|
127
|
+
for (i=0; i<RANDSIZ; ++i) ctx.randrsl[i]=(ub4)0;
|
128
|
+
isaac_init(&ctx, 1);
|
129
|
+
for (i=0; i<2; ++i)
|
130
|
+
{
|
131
|
+
isaac_rand(&ctx);
|
132
|
+
for (j=0; j<RANDSIZ; ++j)
|
133
|
+
{
|
134
|
+
printf("%.8lx",(long unsigned int)ctx.randrsl[j]);
|
135
|
+
if ((j&7)==7) printf("\n");
|
136
|
+
}
|
137
|
+
}
|
138
|
+
}
|
139
|
+
#endif
|
data/ext/isaac/rand.h
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
/*
|
2
|
+
------------------------------------------------------------------------------
|
3
|
+
rand.h: definitions for a random number generator
|
4
|
+
By Bob Jenkins, 1996, Public Domain
|
5
|
+
MODIFIED:
|
6
|
+
960327: Creation (addition of randinit, really)
|
7
|
+
970719: use context, not global variables, for internal state
|
8
|
+
980324: renamed seed to flag
|
9
|
+
980605: recommend RANDSIZL=4 for noncryptography.
|
10
|
+
010626: note this is public domain
|
11
|
+
|
12
|
+
ADAPTED Aug2004 for use in Ruby by Joel VanderWerf
|
13
|
+
ADAPTED Jan2010 for 64 bit systems (same algorithm and results as 32 bit)
|
14
|
+
------------------------------------------------------------------------------
|
15
|
+
*/
|
16
|
+
|
17
|
+
#include <stdint.h>
|
18
|
+
|
19
|
+
typedef uint32_t ub4;
|
20
|
+
|
21
|
+
#ifndef RAND
|
22
|
+
#define RAND
|
23
|
+
#define RANDSIZL (8) /* I recommend 8 for crypto, 4 for simulations */
|
24
|
+
#define RANDSIZ (1<<RANDSIZL)
|
25
|
+
|
26
|
+
/* context of random number generator */
|
27
|
+
struct randctx
|
28
|
+
{
|
29
|
+
ub4 randcnt;
|
30
|
+
ub4 randrsl[RANDSIZ];
|
31
|
+
ub4 randmem[RANDSIZ];
|
32
|
+
ub4 randa;
|
33
|
+
ub4 randb;
|
34
|
+
ub4 randc;
|
35
|
+
};
|
36
|
+
typedef struct randctx randctx;
|
37
|
+
|
38
|
+
/*
|
39
|
+
------------------------------------------------------------------------------
|
40
|
+
If (flag==TRUE), then use the contents of randrsl[0..RANDSIZ-1] as the seed.
|
41
|
+
------------------------------------------------------------------------------
|
42
|
+
*/
|
43
|
+
void isaac_init(randctx *r, int flag);
|
44
|
+
|
45
|
+
void isaac_rand(randctx *r);
|
46
|
+
|
47
|
+
|
48
|
+
/*
|
49
|
+
------------------------------------------------------------------------------
|
50
|
+
Call rand(/o_ randctx *r _o/) to retrieve a single 32-bit random value
|
51
|
+
------------------------------------------------------------------------------
|
52
|
+
*/
|
53
|
+
#define rand(r) \
|
54
|
+
(!(r)->randcnt-- ? \
|
55
|
+
(isaac_rand(r), (r)->randcnt=RANDSIZ-1, (r)->randrsl[(r)->randcnt]) : \
|
56
|
+
(r)->randrsl[(r)->randcnt])
|
57
|
+
|
58
|
+
#endif /* RAND */
|
59
|
+
|
60
|
+
|
data/ext/isaac/rand4.c
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
/*
|
2
|
+
------------------------------------------------------------------------------
|
3
|
+
rand4.c: rand.c specialized to RANDSIZL=4
|
4
|
+
------------------------------------------------------------------------------
|
5
|
+
*/
|
6
|
+
#ifndef RAND4
|
7
|
+
#include "rand4.h"
|
8
|
+
#endif
|
9
|
+
|
10
|
+
|
11
|
+
#define ind(mm,x) ((mm)[(x>>2)&(RANDSIZ4-1)])
|
12
|
+
#define rngstep(mix,a,b,mm,m,m2,r,x) \
|
13
|
+
{ \
|
14
|
+
x = *m; \
|
15
|
+
a = ((a^(mix)) + *(m2++)); \
|
16
|
+
*(m++) = y = (ind(mm,x) + a + b); \
|
17
|
+
*(r++) = b = (ind(mm,y>>4) + x); \
|
18
|
+
}
|
19
|
+
|
20
|
+
void isaac_rand4(ctx)
|
21
|
+
randctx4 *ctx;
|
22
|
+
{
|
23
|
+
register ub4 a,b,x,y,*m,*mm,*m2,*r,*mend;
|
24
|
+
mm=ctx->randmem; r=ctx->randrsl;
|
25
|
+
a = ctx->randa; b = (ctx->randb + (++ctx->randc));
|
26
|
+
for (m = mm, mend = m2 = m+(RANDSIZ4/2); m<mend; )
|
27
|
+
{
|
28
|
+
rngstep( a<<13, a, b, mm, m, m2, r, x);
|
29
|
+
rngstep( a>>6 , a, b, mm, m, m2, r, x);
|
30
|
+
rngstep( a<<2 , a, b, mm, m, m2, r, x);
|
31
|
+
rngstep( a>>16, a, b, mm, m, m2, r, x);
|
32
|
+
}
|
33
|
+
for (m2 = mm; m2<mend; )
|
34
|
+
{
|
35
|
+
rngstep( a<<13, a, b, mm, m, m2, r, x);
|
36
|
+
rngstep( a>>6 , a, b, mm, m, m2, r, x);
|
37
|
+
rngstep( a<<2 , a, b, mm, m, m2, r, x);
|
38
|
+
rngstep( a>>16, a, b, mm, m, m2, r, x);
|
39
|
+
}
|
40
|
+
ctx->randb = b; ctx->randa = a;
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
#define mix(a,b,c,d,e,f,g,h) \
|
45
|
+
{ \
|
46
|
+
a^=b<<11; d+=a; b+=c; \
|
47
|
+
b^=c>>2; e+=b; c+=d; \
|
48
|
+
c^=d<<8; f+=c; d+=e; \
|
49
|
+
d^=e>>16; g+=d; e+=f; \
|
50
|
+
e^=f<<10; h+=e; f+=g; \
|
51
|
+
f^=g>>4; a+=f; g+=h; \
|
52
|
+
g^=h<<8; b+=g; h+=a; \
|
53
|
+
h^=a>>9; c+=h; a+=b; \
|
54
|
+
}
|
55
|
+
|
56
|
+
/* if (flag==TRUE), then use the contents of randrsl[] to initialize mm[]. */
|
57
|
+
void isaac_init4(ctx, flag)
|
58
|
+
randctx4 *ctx;
|
59
|
+
int flag;
|
60
|
+
{
|
61
|
+
int i;
|
62
|
+
ub4 a,b,c,d,e,f,g,h;
|
63
|
+
ub4 *m,*r;
|
64
|
+
ctx->randa = ctx->randb = ctx->randc = 0;
|
65
|
+
m=ctx->randmem;
|
66
|
+
r=ctx->randrsl;
|
67
|
+
a=b=c=d=e=f=g=h=0x9e3779b9; /* the golden ratio */
|
68
|
+
|
69
|
+
for (i=0; i<4; ++i) /* scramble it */
|
70
|
+
{
|
71
|
+
mix(a,b,c,d,e,f,g,h);
|
72
|
+
}
|
73
|
+
|
74
|
+
if (flag)
|
75
|
+
{
|
76
|
+
/* initialize using the contents of r[] as the seed */
|
77
|
+
for (i=0; i<RANDSIZ4; i+=8)
|
78
|
+
{
|
79
|
+
a+=r[i ]; b+=r[i+1]; c+=r[i+2]; d+=r[i+3];
|
80
|
+
e+=r[i+4]; f+=r[i+5]; g+=r[i+6]; h+=r[i+7];
|
81
|
+
mix(a,b,c,d,e,f,g,h);
|
82
|
+
m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
|
83
|
+
m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
|
84
|
+
}
|
85
|
+
/* do a second pass to make all of the seed affect all of m */
|
86
|
+
for (i=0; i<RANDSIZ4; i+=8)
|
87
|
+
{
|
88
|
+
a+=m[i ]; b+=m[i+1]; c+=m[i+2]; d+=m[i+3];
|
89
|
+
e+=m[i+4]; f+=m[i+5]; g+=m[i+6]; h+=m[i+7];
|
90
|
+
mix(a,b,c,d,e,f,g,h);
|
91
|
+
m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
|
92
|
+
m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
else
|
96
|
+
{
|
97
|
+
/* fill in mm[] with messy stuff */
|
98
|
+
for (i=0; i<RANDSIZ4; i+=8)
|
99
|
+
{
|
100
|
+
mix(a,b,c,d,e,f,g,h);
|
101
|
+
m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
|
102
|
+
m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
isaac_rand4(ctx); /* fill in the first set of results */
|
107
|
+
ctx->randcnt=RANDSIZ4; /* prepare to use the first set of results */
|
108
|
+
}
|
109
|
+
|
110
|
+
|
111
|
+
#ifdef NEVER
|
112
|
+
#include <stdio.h>
|
113
|
+
|
114
|
+
int main()
|
115
|
+
{
|
116
|
+
ub4 i,j;
|
117
|
+
randctx4 ctx;
|
118
|
+
ctx.randa=ctx.randb=ctx.randc=(ub4)0;
|
119
|
+
for (i=0; i<RANDSIZ4; ++i) ctx.randrsl[i]=(ub4)0;
|
120
|
+
isaac_init4(&ctx, 1);
|
121
|
+
for (i=0; i<2; ++i)
|
122
|
+
{
|
123
|
+
isaac_rand4(&ctx);
|
124
|
+
for (j=0; j<RANDSIZ4; ++j)
|
125
|
+
{
|
126
|
+
printf("%.8lx",(long unsigned int)ctx.randrsl[j]);
|
127
|
+
if ((j&7)==7) printf("\n");
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
131
|
+
#endif
|
data/ext/isaac/rand4.h
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
/*
|
2
|
+
------------------------------------------------------------------------------
|
3
|
+
rand4.h: rand.h specialized to RANDSIZL=4
|
4
|
+
------------------------------------------------------------------------------
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <stdint.h>
|
8
|
+
|
9
|
+
#include "rand.h"
|
10
|
+
|
11
|
+
#ifndef RAND4
|
12
|
+
#define RAND4
|
13
|
+
#define RANDSIZ4 (1<<4)
|
14
|
+
|
15
|
+
/* context of random number generator */
|
16
|
+
struct randctx4
|
17
|
+
{
|
18
|
+
ub4 randcnt;
|
19
|
+
ub4 randrsl[RANDSIZ4];
|
20
|
+
ub4 randmem[RANDSIZ4];
|
21
|
+
ub4 randa;
|
22
|
+
ub4 randb;
|
23
|
+
ub4 randc;
|
24
|
+
};
|
25
|
+
typedef struct randctx4 randctx4;
|
26
|
+
|
27
|
+
/*
|
28
|
+
------------------------------------------------------------------------------
|
29
|
+
If (flag==TRUE), then use the contents of randrsl[0..RANDSIZ4-1] as the seed.
|
30
|
+
------------------------------------------------------------------------------
|
31
|
+
*/
|
32
|
+
void isaac_init4(randctx4 *r, int flag);
|
33
|
+
|
34
|
+
void isaac_rand4(randctx4 *r);
|
35
|
+
|
36
|
+
|
37
|
+
/*
|
38
|
+
------------------------------------------------------------------------------
|
39
|
+
Call rand4(/o_ randctx4 *r _o/) to retrieve a single 32-bit random value
|
40
|
+
------------------------------------------------------------------------------
|
41
|
+
*/
|
42
|
+
#define rand4(r) \
|
43
|
+
(!(r)->randcnt-- ? \
|
44
|
+
(isaac_rand4(r), (r)->randcnt=RANDSIZ4-1, (r)->randrsl[(r)->randcnt]) : \
|
45
|
+
(r)->randrsl[(r)->randcnt])
|
46
|
+
|
47
|
+
#endif /* RAND4 */
|
48
|
+
|
49
|
+
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tuplex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel VanderWerf
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Constructs index keys for tuples.
|
14
|
+
email: vjoel@users.sourceforge.net
|
15
|
+
executables: []
|
16
|
+
extensions:
|
17
|
+
- ext/isaac/extconf.rb
|
18
|
+
extra_rdoc_files:
|
19
|
+
- README.md
|
20
|
+
- COPYING
|
21
|
+
files:
|
22
|
+
- COPYING
|
23
|
+
- README.md
|
24
|
+
- ext/isaac/extconf.rb
|
25
|
+
- ext/isaac/isaac.c
|
26
|
+
- ext/isaac/rand.c
|
27
|
+
- ext/isaac/rand.h
|
28
|
+
- ext/isaac/rand4.c
|
29
|
+
- ext/isaac/rand4.h
|
30
|
+
- lib/tuplex/version.rb
|
31
|
+
homepage: https://github.com/vjoel/tuplex
|
32
|
+
licenses: []
|
33
|
+
metadata: {}
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- "--quiet"
|
37
|
+
- "--line-numbers"
|
38
|
+
- "--inline-source"
|
39
|
+
- "--title"
|
40
|
+
- Tuplex
|
41
|
+
- "--main"
|
42
|
+
- README.md
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
- ext
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - "~>"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '2.0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 2.4.1
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: Tuple index.
|
62
|
+
test_files: []
|
63
|
+
has_rdoc:
|