ibba随机数生成器

 

java版本:

////////////////////////////////////////////////////////

package org.haifi.util;

public class IbaaRandom {
private final static int ALPHA = 8;
private final static int SIZE = (1 << ALPHA);

public int m[];
private int r[];
private int r_sz;
private int aa;
private int bb;

public IbaaRandom(int seed) {
m = new int[SIZE];
r = new int[SIZE];
r_sz = 0;

setSeed(seed);
}

/*
* Straight forward port of Robert Jenkin Jr.’s reference IBAA
* implementation. Original: http://burtleburtle.net/bob/rand/isaac.html
*/
private final void ibaa() {
int a, b, x, y;
int t1, t2;
a = aa;
b = bb;
for (int i = 0; i < SIZE; i++) {
x = m[i];
// 神奇了,不知道是不是位操作符的原因,为什么被注释掉的这一行和下面两行的结果不相同
// a = ((a) << 19) ^ ((a) >>> 13) + m[(i + (SIZE / 2)) & (SIZE –
// 1)];
t1 = ((a) << 19) ^ ((a) >>> 13);
t2 = m[(i + (SIZE / 2)) & (SIZE – 1)];
a = t1 + t2;

m[i] = y = m[((x) & (SIZE – 1))] + a + b;
r[i] = b = m[((y >>> ALPHA) & (SIZE – 1))] + x;
}
bb = b;
aa = a;
}

// private final void ibaa() {
// long a, b, x, y;
//
// a = aa;
// b = bb;
// for (int i = 0; i < SIZE; i++) {
// x = m[i];
// a = ((a) << 19) ^ ((a) >>> 13) + m[(i + (SIZE / 2)) & (SIZE – 1)];
// y = m[(int) (((x) & (SIZE – 1)))] + a + b;
// m[i] = (int) y;
// b = m[(int) (((y >>> ALPHA) & (SIZE – 1)))] + x;
// r[i] = (int) b;
// }
// bb = (int) b;
// aa = (int) a;
// }

protected final int next() {
return next(32);
}

protected final int next(int nrBits) {
if (r_sz– == 0) {
ibaa();
r_sz = SIZE – 1;
}
return r[255 – r_sz] >>> (32 – nrBits);
}

public void setSeed(int seed) {
int rng = seed;

// Crappy PRNG cribbed from the commons-math MT seed routine.
for (int i = 0; i < SIZE; i++) {
rng = ((int) ((int) (1812433253l * (rng ^ (rng >> 30))) + i) & 0xffffffff);
m[i] = (int) rng;
}
}

public void setSeed(int[] seed) {
for (int i = 0; i < seed.length; i++)
m[i] = seed[i];
}

public void setSeed(long seed) {
setSeed((int) seed);
}
}

// 测试
public static void main(String[] args) {
IbaaRandom random = new IbaaRandom(1000);
for (int m : random.m) {
System.out.print(m + ” “);
}
System.out.println();
for (int m = 0; m < 550; m++) {
System.out.printf(“%d %X “, m, random.next());
// System.out.print(random.next()+” “);
}

}

 

测试结果:

0 27F8A7E5 1 F2AA7AB0 2 63163913 3 827077 4 B658A81E 5 525C5E6B 6 9ACE0E23 7 C6017783 8 4BAB1F55 9 CADC61DD 10 991130EA 11 8F34CC45 12 BD2B0273 13 420B9996 14 32B0C8DC 15 876209EF 16 E3B67B8D 17 A0F2C4ED 18 9E78028B 19 22C75256 20 AB11A849 21 8ADB8CCE 22 2928DCC7 23 F16658C3 24 A5C77464 25 F7B5C36D 26 F331B0C6 27 63E97BC9 28 917915E4 29 D8D52815 30 286B0FBC 31 5D0A5418 32 9AC68E16 33 1AD0EAAD 34 AD39CD2C 35 CEA8CE3C 36 5C306B47 37 CBFB3050 38 D4FF10DE 39 17E3D476 40 D916EA2A 41 3ED28202 42 5B89D229 43 246E2785 44 80CBBB0A 45 471E15AE 46 8E737B2A 47 BA9AAA1B 48 625B8410 49 4BC69E78 50 1B2F30F 51 DCA3BC6D 52 EFA23A99 53 488D366A 54 DEA22A9A 55 1CDE510D 56 92D5AB93 57 7ABF1DD5 58 D9F4BCEC 59 CB1A3CCB 60 2BBE6DC 61 45AB1CA0 62 289C3F18 63 4AA5D5CB 64 DA68F277 65 E7FA890D 66 341637D8 67 6C88803 68 BA978476 69 BB13DA1 70 F768A913 71 533E43DF 72 1BBCF969 73 E45D7FC6 74 32C48D65 75 1FB68EFA 76 9345FAEB 77 6AE716D 78 E89EA993 79 FFE3FEA5 80 355BA3CC 81 854B1BB1 82 7D96D8BC 83 8F8F155 84 68DAF8AF 85 22B1D9BC 86 CD283CF2 87 52F77D4C 88 256E61EC 89 3623267 90 E014E3E6 91 D6A3686E 92 A309198E 93 13E95414 94 78725AB6 95 92D8E162 96 FAF86230 97 EEA5A413 98 EAB3FB09 99 63E362A2 100 11EC33F4 101 5F972C0 102 A1DD43F3 103 19D24FD4 104 E487EF58 105 6068423D 106 B0F8BF6 107 99603DCF 108 6B6A756 109 4C91AC85 110 A06C6A1 111 39EC9682 112 33971E74 113 902271D9 114 FF9AC586 115 97233AF2 116 277BA0A 117 E38D557D 118 D3E9E7E2 119 5833239E 120 3D190227 121 FE51EE7A 122 2E929D01 123 F83DC7A7 124 16CECD94 125 60413348 126 8859ADF0 127 39F4B391 128 36832F54 129 7B891B43 130 DBC2FA3A 131 88990C3C 132 F11E9026 133 B40B260D 134 C476AB1D 135 CEB262BD 136 B24273AD 137 EEEEB76F 138 38854D82 139 85868893 140 BC32EF6C 141 9A69402A 142 4EC507BF 143 543985AE 144 61C53E78 145 C53E7AC3 146 362BA019 147 D739DB0 148 AA0CCBA2 149 59EF3DB5 150 531F27CB 151 2C2278B5 152 7557D3AE 153 47FCA7B3 154 8E40A5E8 155 6396A502 156 999F09AC 157 DD600324 158 D97AD68E 159 8B130874 160 916AE93B 161 D1BB60D3 162 7F49182A 163 BF775D49 164 61BEEF31 165 BC9508FB 166 55D439C 167 8FEF79CB 168 ED543C44 169 72754F1D 170 15DC80AE 171 EAD8DDC 172 7901047D 173 85DC0993 174 8BF249BB 175 E89C1FEA 176 E1703C11 177 EE2B6A60 178 80A8D6AD 179 2D651811 180 7F26EB41 181 92E878F6 182 43C4E82F 183 2D66D36B 184 41F11F82 185 E1FC2B68 186 7614CD93 187 DD0E8603 188 C41E0654 189 2F88931C 190 A42B7A2F 191 F24F9E8C 192 84AFABE2 193 340A3A31 194 6CBA5BCC 195 95CA4F55 196 B5949355 197 C43D257A 198 DD7F668 199 DF2E2E9C 200 9D84225E 201 1F70DB93 202 E5EF2FC6 203 B41A1453 204 D825699E 205 E8709FAE 206 9DE664AF 207 9C85F9A1 208 5189450C 209 AF846CBC 210 2F06FAE2 211 82FF79A 212 83FF44A4 213 C6FA458C 214 5F052A3C 215 D5697CCC 216 2C4E2FC8 217 6C66B4B1 218 F77A668C 219 70AC1208 220 D1FE59AF 221 A022577E 222 A04BCEB9 223 7A1308A 224 992CB652 225 D4B18B87 226 1F63A5A8 227 5CF62694 228 7D27CFE2 229 1BB3C11A 230 D2E56177 231 3C3BA0B1 232 C64223E7 233 DA48FCED 234 91484E48 235 A539CF04 236 80A00CF3 237 7DD3AF44 238 BBA255E0 239 840FE9E9 240 635F55C2 241 4C908F06 242 C1495737 243 DD1B486F 244 D844884C 245 FA2521C 246 12695CD9 247 2C496BFB 248 34670758 249 7747DB49 250 50C485AB 251 98E93F3B 252 AECA4AA 253 54F60444 254 61BB9EFA 255 32969F 256 4A97A653 257 D8047AB0 258 71E216B7 259 A074D54 260 42E39469 261 CFAB1515 262 E03C1F56 263 E6F9E01 264 D89858CF 265 787A23F 266 AE42CB0B 267 596E7801 268 3FEC0F05 269 E9882B6F 270 5531AC50 271 1AFD0D74 272 96724FAD 273 37DF278 274 7B78623 275 39D24D87 276 9D72F3AB 277 72188FE0 278 9851125B 279 E5AD6F0D 280 F31DF7A9 281 36F13A72 282 849014E0 283 CDCC191F 284 49DDE2FA 285 CE4DB349 286 195FF3A8 287 85B52014 288 773DD9E2 289 EABC9F2C 290 15304FE3 291 2639062A 292 2BA4C50 293 5BA3ABC5 294 BF985A0C 295 653AEF26 296 331FF37 297 80103B40 298 6658E116 299 54E93B1D 300 3DEE2E47 301 BBF646E3 302 B00790D2 303 50A61323 304 66B95F0A 305 26E47C31 306 701313A3 307 C7C4A9B4 308 239C7325 309 6EF399DD 310 687DBD35 311 6EFA82DB 312 ADBF4555 313 3D5BD1D 314 B4316E02 315 D943AD93 316 3188A39D 317 54F96B74 318 22BB8448 319 1B1EC120 320 50EB3466 321 F0569298 322 E91607AE 323 1CB433B5 324 6161DA75 325 4DCCD78C 326 6CF3B4CD 327 9E719B5B 328 CBEA19E3 329 EF4DF09E 330 1F9B3A53 331 431D8FCC 332 E33F8C2C 333 DCE917E0 334 2E752AF8 335 454870E3 336 7A55F926 337 58EB123A 338 860DB140 339 D09B0B95 340 3D47F262 341 7AF65014 342 C1C5CC91 343 A87282F0 344 9369308A 345 6D2A5F0E 346 BF124226 347 2E86E2EE 348 3AF354F1 349 6369618F 350 403BCC2 351 CB67759B 352 41D9C2D4 353 8AC5F428 354 707E26A7 355 CECB73A9 356 CD6E45A 357 2200111C 358 868356CF 359 D617A247 360 D2E10EEA 361 C859835A 362 978235DF 363 43A886EA 364 110D4238 365 85D58851 366 CD7FC05D 367 A539F322 368 17BB7FD9 369 288554AC 370 E48DF8CF 371 D5FFA158 372 68B3C7AD 373 C0E6FDF7 374 54199199 375 1F4E286 376 FE0C5A34 377 A7A088DD 378 8F7FE367 379 725B26C 380 9C9EFFDE 381 958257D6 382 D48A8124 383 2550BCEF 384 DA97C3BE 385 C1C70587 386 5A9A44C2 387 892A63D5 388 2F46F0C 389 BEE58F2B 390 8645627F 391 172A7AA1 392 6DF88215 393 1B1B9E1A 394 8C97F982 395 9F32F042 396 52FCBE66 397 E6A0DECE 398 547D95B3 399 5A38792A 400 991F9348 401 E29AD9BA 402 35EF8112 403 6747B756 404 7D6EF7B5 405 3FBE904E 406 5A07A63A 407 6C2BD3B5 408 5DAE6D50 409 38DC982F 410 1B3A9650 411 23F018F8 412 4A77D853 413 6A91FF05 414 353C333E 415 66CA0499 416 5B76F430 417 3F0ED272 418 13C77314 419 342096CD 420 BA1A5E4D 421 35D77B57 422 510A8E08 423 E9B7E673 424 B60DD64A 425 54E858E2 426 F8FA61E7 427 EE1FE4E2 428 4CB884D2 429 39CA39A5 430 E3854A99 431 AA87A575 432 60E18621 433 9EB7B5BD 434 A9853190 435 956B7CB5 436 A7CF0B48 437 FD920C63 438 203B7E99 439 7B2D0E51 440 BFA33FFB 441 E39AD8FC 442 F5B8AE52 443 51F28F4F 444 43275579 445 FB13DF3B 446 93EBE56D 447 782B1E9E 448 87AA5F43 449 56DA0C1D 450 94B07CAA 451 A6678A23 452 D3E85D73 453 620622FA 454 2DBFC47F 455 554CD1E8 456 2BD3E21 457 9E7C2715 458 152BA9BC 459 ACE84B61 460 B7F34734 461 CA45BDD8 462 F13A73BC 463 46E52B72 464 7ED92F83 465 2FC01C35 466 C4100830 467 87D82EEF 468 55DEB295 469 C060D47B 470 14ADDED5 471 2FB2F237 472 A0342C8F 473 1CA25DA5 474 3B420907 475 2B8D65E2 476 9551424A 477 DF77471B 478 5EA0E44E 479 F1C6C7D2 480 C2511452 481 2F458647 482 9A5C6279 483 F721F261 484 30D1FAC2 485 7CB25003 486 6E0BDF6C 487 CBC6F43C 488 82FA406A 489 BC966110 490 C0E274C2 491 7BC8880A 492 6F237788 493 1291C32E 494 7694A7C8 495 8FF34F53 496 2F525782 497 E88F706A 498 8BF71F4A 499 1F9F4192 500 26FECAFE 501 8BF56109 502 4E533999 503 C6D4C3A8 504 9B2E4AA5 505 8AD56FB8 506 8C39A9A8 507 69D2DAFA 508 DA440898 509 E74A0F56 510 5D645A8F 511 A7AE0029 512 2455EC3E 513 7A74061B 514 77F56304 515 62406669 516 23EC3182 517 40367827 518 78861790 519 A2318184 520 19919361 521 43B3872 522 DBF60FF0 523 D5C59416 524 F81AC1EE 525 C66D0EA1 526 B637654F 527 3227693 528 763D437C 529 B17A9AD7 530 8F54513F 531 25BF793D 532 3876568D 533 1726E6A4 534 7D77F11C 535 96B05F5C 536 6CA2963B 537 1C21CE7C 538 3E16A81D 539 E6D9C12F 540 1F53202F 541 CC405CEC 542 9EAC52AB 543 A41AEDB1 544 BC14DA42 545 FD74C13E 546 9F55A67A 547 AE0D968E 548 B32365A8 549 693CB072

 

///////////////////////////////////////////////////////////////////////

c 代码:

typedef  unsigned int  u4;   /* unsigned four bytes, 32 bits */
#define ALPHA      (8)
#define SIZE       (1<<ALPHA)
#define ind(x)     ((x)&(SIZE-1))
#define barrel(a)  (((a)<<19)^((a)>>13)) /* beta=32,shift=19 */

static u4 m[SIZE];
static u4 r[SIZE];
static u4 aa=0;
static u4 bb=0;
static int r_sz=0;

static void setSeed(int seed){
memset(m,0,SIZE);
memset(r,0,SIZE);
aa=0;
bb=0;
r_sz=0;
long rng = seed;

// Crappy PRNG cribbed from the commons-math MT seed routine.
for (int i = 0; i < SIZE; i++) {
rng = (1812433253l * (rng ^ (rng >> 30)) + i) & 0xffffffffL;
m[i] = (int) rng;
}
}

static void ibaa(u4* m,u4* r,u4* aa,u4* bb)
// u4 *m;   /* Memory: array of SIZE ALPHA-bit terms */
// u4 *r;   /* Results: the sequence, same size as m */
// u4 *aa;  /* Accumulator: a single value */
// u4 *bb;  /* the previous result */
{
register u4 a,b,x,y,i;
u4 t1,t2;
a = *aa; b = *bb;
for (i=0; i<SIZE; ++i)
{
x = m[i];
//a = barrel(a) + m[ind(i+(SIZE/2))];    /* set a */
t1=barrel(a);
t2=m[ind(i+(SIZE/2))];
a=t1+t2;
m[i] = y = m[ind(x)] + a + b;          /* set m */
r[i] = b = m[ind(y>>ALPHA)] + x;       /* set r */
}
*bb = b; *aa = a;
}

static u4 next() {
if (r_sz– == 0) {
ibaa(m,r,&aa,&bb);
r_sz = SIZE – 1;
}
return r[255-r_sz];
}

 

 

发表评论?

0 条评论。

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>