1 package org.jahia.utils.keygenerator; 2 3 4 import java.io.Serializable ; 5 6 91 92 93 94 public class MersenneTwisterFast implements Serializable 95 { 96 private static final int N = 624; 98 private static final int M = 397; 99 private static final int MATRIX_A = 0x9908b0df; private static final int UPPER_MASK = 0x80000000; private static final int LOWER_MASK = 0x7fffffff; 103 104 private static final int TEMPERING_MASK_B = 0x9d2c5680; 106 private static final int TEMPERING_MASK_C = 0xefc60000; 107 108 113 private int mt[]; private int mti; private int mag01[]; 116 117 private static final long GOOD_SEED = 4357; 119 120 private double nextNextGaussian; 121 private boolean haveNextNextGaussian; 122 123 124 128 public MersenneTwisterFast() 129 { 130 setSeed(GOOD_SEED); 131 } 132 133 139 public MersenneTwisterFast(long seed) 140 { 141 setSeed(seed); 142 } 143 144 153 public final void setSeed(long seed) 154 { 155 haveNextNextGaussian = false; 156 157 mt = new int[N]; 158 159 164 167 mt[0]= ((int)seed); 169 for (mti = 1; mti < N; mti++) 170 mt[mti] = (69069 * mt[mti-1]); 172 mag01 = new int[2]; 174 mag01[0] = 0x0; 175 mag01[1] = MATRIX_A; 176 } 177 178 public final int nextInt() 179 { 180 int y; 181 182 if (mti >= N) { 184 int kk; 185 186 for (kk = 0; kk < N - M; kk++) 187 { 188 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 189 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 190 } 191 for (; kk < N-1; kk++) 192 { 193 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 194 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 195 } 196 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 197 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 198 199 mti = 0; 200 } 201 202 203 y = mt[mti++]; 204 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 209 return y; 210 } 211 212 213 214 public final short nextShort() 215 { 216 int y; 217 218 if (mti >= N) { 220 int kk; 221 222 for (kk = 0; kk < N - M; kk++) 223 { 224 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 225 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 226 } 227 for (; kk < N-1; kk++) 228 { 229 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 230 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 231 } 232 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 233 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 234 235 236 mti = 0; 237 } 238 239 y = mt[mti++]; 240 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 245 return (short)(y >>> 16); 246 } 247 248 249 250 public final char nextChar() 251 { 252 int y; 253 254 if (mti >= N) 256 { 257 int kk; 258 259 for (kk = 0; kk < N - M; kk++) 260 { 261 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 262 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 263 } 264 for (; kk < N-1; kk++) 265 { 266 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 267 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 268 } 269 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 270 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 271 272 mti = 0; 273 } 274 275 y = mt[mti++]; 276 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 281 return (char)(y >>> 16); 282 } 283 284 285 public final boolean nextBoolean() 286 { 287 int y; 288 289 if (mti >= N) { 291 int kk; 292 293 for (kk = 0; kk < N - M; kk++) 294 { 295 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 296 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 297 } 298 299 for (; kk < N-1; kk++) 300 { 301 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 302 303 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 304 } 305 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 306 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 307 308 mti = 0; 309 } 310 311 y = mt[mti++]; 312 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 317 return (boolean)((y >>> 31) != 0); 318 } 319 320 321 public final byte nextByte() 322 { 323 int y; 324 325 if (mti >= N) { 327 int kk; 328 329 for (kk = 0; kk < N - M; kk++) 330 { 331 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 332 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 333 } 334 for (; kk < N-1; kk++) 335 { 336 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 337 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 338 } 339 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 340 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 341 342 mti = 0; 343 } 344 345 y = mt[mti++]; 346 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 351 return (byte)(y >>> 24); 352 } 353 354 355 public final void nextBytes(byte[] bytes) 356 { 357 int y; 358 359 for (int x=0;x<bytes.length;x++) 360 { 361 if (mti >= N) { 363 int kk; 364 365 for (kk = 0; kk < N - M; kk++) 366 { 367 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 368 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 369 } 370 for (; kk < N-1; kk++) 371 { 372 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 373 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 374 } 375 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 376 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 377 378 mti = 0; 379 } 380 381 y = mt[mti++]; 382 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 387 bytes[x] = (byte)(y >>> 24); 388 } 389 } 390 391 392 public final long nextLong() 393 { 394 int y; 395 int z; 396 397 if (mti >= N) { 399 int kk; 400 401 for (kk = 0; kk < N - M; kk++) 402 { 403 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 404 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 405 } 406 for (; kk < N-1; kk++) 407 { 408 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 409 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 410 } 411 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 412 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 413 414 415 mti = 0; 416 } 417 418 y = mt[mti++]; 419 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 424 if (mti >= N) 426 { 427 int kk; 428 429 for (kk = 0; kk < N - M; kk++) 430 { 431 z = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 432 mt[kk] = mt[kk+M] ^ (z >>> 1) ^ mag01[z & 0x1]; 433 } 434 for (; kk < N-1; kk++) 435 { 436 z = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 437 mt[kk] = mt[kk+(M-N)] ^ (z >>> 1) ^ mag01[z & 0x1]; 438 } 439 z = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 440 mt[N-1] = mt[M-1] ^ (z >>> 1) ^ mag01[z & 0x1]; 441 442 mti = 0; 443 } 444 445 z = mt[mti++]; 446 z ^= z >>> 11; z ^= (z << 7) & TEMPERING_MASK_B; z ^= (z << 15) & TEMPERING_MASK_C; z ^= (z >>> 18); 451 return (((long)y) << 32) + (long)z; 452 } 453 454 455 public final double nextDouble() 456 { 457 int y; 458 int z; 459 460 if (mti >= N) { 462 int kk; 463 464 for (kk = 0; kk < N - M; kk++) 465 { 466 467 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 468 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 469 } 470 for (; kk < N-1; kk++) 471 { 472 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 473 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 474 } 475 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 476 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 477 478 mti = 0; 479 } 480 481 y = mt[mti++]; 482 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 487 if (mti >= N) { 489 int kk; 490 491 for (kk = 0; kk < N - M; kk++) 492 { 493 z = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 494 mt[kk] = mt[kk+M] ^ (z >>> 1) ^ mag01[z & 0x1]; 495 } 496 for (; kk < N-1; kk++) 497 { 498 z = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 499 mt[kk] = mt[kk+(M-N)] ^ (z >>> 1) ^ mag01[z & 0x1]; 500 } 501 z = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 502 503 mt[N-1] = mt[M-1] ^ (z >>> 1) ^ mag01[z & 0x1]; 504 505 mti = 0; 506 } 507 508 z = mt[mti++]; 509 z ^= z >>> 11; z ^= (z << 7) & TEMPERING_MASK_B; z ^= (z << 15) & TEMPERING_MASK_C; z ^= (z >>> 18); 514 515 return ((((long)(y >>> 6)) << 27) + (z >>> 5)) / (double)(1L << 53); 516 } 517 518 519 520 521 522 public final double nextGaussian() 523 { 524 if (haveNextNextGaussian) 525 { 526 haveNextNextGaussian = false; 527 return nextNextGaussian; 528 } 529 else 530 { 531 double v1, v2, s; 532 do 533 { 534 int y; 535 int z; 536 int a; 537 int b; 538 539 if (mti >= N) { 541 int kk; 542 543 for (kk = 0; kk < N - M; kk++) 544 545 { 546 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 547 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 548 } 549 for (; kk < N-1; kk++) 550 { 551 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 552 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 553 } 554 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 555 556 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 557 558 mti = 0; 559 } 560 561 y = mt[mti++]; 562 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 567 if (mti >= N) { 569 int kk; 570 571 for (kk = 0; kk < N - M; kk++) 572 { 573 z = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 574 mt[kk] = mt[kk+M] ^ (z >>> 1) ^ mag01[z & 0x1]; 575 } 576 for (; kk < N-1; kk++) 577 { 578 z = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 579 mt[kk] = mt[kk+(M-N)] ^ (z >>> 1) ^ mag01[z & 0x1]; 580 } 581 z = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 582 mt[N-1] = mt[M-1] ^ (z >>> 1) ^ mag01[z & 0x1]; 583 584 mti = 0; 585 } 586 587 z = mt[mti++]; 588 z ^= z >>> 11; z ^= (z << 7) & TEMPERING_MASK_B; z ^= (z << 15) & TEMPERING_MASK_C; z ^= (z >>> 18); 593 if (mti >= N) { 595 int kk; 596 597 for (kk = 0; kk < N - M; kk++) 598 { 599 a = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 600 mt[kk] = mt[kk+M] ^ (a >>> 1) ^ mag01[a & 0x1]; 601 } 602 for (; kk < N-1; kk++) 603 { 604 605 a = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 606 mt[kk] = mt[kk+(M-N)] ^ (a >>> 1) ^ mag01[a & 0x1]; 607 } 608 a = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 609 mt[N-1] = mt[M-1] ^ (a >>> 1) ^ mag01[a & 0x1]; 610 611 mti = 0; 612 } 613 614 a = mt[mti++]; 615 a ^= a >>> 11; a ^= (a << 7) & TEMPERING_MASK_B; a ^= (a << 15) & TEMPERING_MASK_C; a ^= (a >>> 18); 620 if (mti >= N) { 622 int kk; 623 624 for (kk = 0; kk < N - M; kk++) 625 { 626 b = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 627 mt[kk] = mt[kk+M] ^ (b >>> 1) ^ mag01[b & 0x1]; 628 } 629 for (; kk < N-1; kk++) 630 { 631 b = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 632 mt[kk] = mt[kk+(M-N)] ^ (b >>> 1) ^ mag01[b & 0x1]; 633 } 634 b = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 635 mt[N-1] = mt[M-1] ^ (b >>> 1) ^ mag01[b & 0x1]; 636 637 mti = 0; 638 } 639 640 b = mt[mti++]; 641 b ^= b >>> 11; b ^= (b << 7) & TEMPERING_MASK_B; b ^= (b << 15) & TEMPERING_MASK_C; b ^= (b >>> 18); 646 647 v1 = 2 * 648 (((((long)(y >>> 6)) << 27) + (z >>> 5)) / (double)(1L << 53)) 649 - 1; 650 v2 = 2 * (((((long)(a >>> 6)) << 27) + (b >>> 5)) / (double)(1L << 53)) 651 - 1; 652 s = v1 * v1 + v2 * v2; 653 } while (s >= 1); 654 double multiplier = Math.sqrt(-2 * Math.log(s)/s); 655 nextNextGaussian = v2 * multiplier; 656 haveNextNextGaussian = true; 657 return v1 * multiplier; 658 } 659 } 660 661 662 663 664 665 666 667 668 669 670 671 672 673 public final float nextFloat() 674 { 675 int y; 676 677 if (mti >= N) { 679 int kk; 680 681 for (kk = 0; kk < N - M; kk++) 682 { 683 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 684 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 685 } 686 for (; kk < N-1; kk++) 687 { 688 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 689 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 690 } 691 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 692 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 693 694 mti = 0; 695 696 } 697 698 y = mt[mti++]; 699 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 704 return (y >>> 8) / ((float)(1 << 24)); 705 } 706 707 708 709 711 public int nextInt(int n) 712 { 713 if (n<=0) 714 throw new IllegalArgumentException ("n must be positive"); 715 716 if ((n & -n) == n) { 718 int y; 719 720 if (mti >= N) { 722 int kk; 723 724 for (kk = 0; kk < N - M; kk++) 725 { 726 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 727 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 728 } 729 for (; kk < N-1; kk++) 730 { 731 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 732 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 733 } 734 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 735 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 736 737 mti = 0; 738 } 739 740 y = mt[mti++]; 741 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 746 return (int)((n * (long) (y >>> 1) ) >> 31); 747 } 748 749 int bits, val; 750 do 751 { 752 int y; 753 754 if (mti >= N) { 756 int kk; 757 758 for (kk = 0; kk < N - M; kk++) 759 { 760 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 761 mt[kk] = mt[kk+M] ^ (y >>> 1) ^ mag01[y & 0x1]; 762 } 763 for (; kk < N-1; kk++) 764 { 765 y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK); 766 mt[kk] = mt[kk+(M-N)] ^ (y >>> 1) ^ mag01[y & 0x1]; 767 } 768 y = (mt[N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK); 769 mt[N-1] = mt[M-1] ^ (y >>> 1) ^ mag01[y & 0x1]; 770 771 mti = 0; 772 } 773 774 y = mt[mti++]; 775 y ^= y >>> 11; y ^= (y << 7) & TEMPERING_MASK_B; y ^= (y << 15) & TEMPERING_MASK_C; y ^= (y >>> 18); 780 bits = (y >>> 1); 781 val = bits % n; 782 } while(bits - val + (n-1) < 0); 783 return val; 784 } 785 786 787 788 789 790 791 792 793 794 797 public static void main(String args[]) 798 { 799 int j; 800 801 MersenneTwisterFast r; 802 803 805 816 817 818 821 837 838 839 841 857 858 859 862 864 960 } 961 962 } 963 | Popular Tags |