Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2009
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.21 KB | None | 0 0
  1. /**
  2. ------------------------------------------------------------------------------
  3. Rand.java: By Bob Jenkins.  My random number generator, ISAAC.
  4.   rand.init() -- initialize
  5.   rand.val()  -- get a random value
  6. MODIFIED:
  7.   960327: Creation (addition of randinit, really)
  8.   970719: use context, not global variables, for internal state
  9.   980224: Translate to Java
  10. ------------------------------------------------------------------------------
  11. */
  12.  
  13. public class Rand {
  14.   final static int SIZEL = 8;              /* log of size of rsl[] and mem[] */
  15.   final static int SIZE = 1<<SIZEL;               /* size of rsl[] and mem[] */
  16.   final static int MASK = (SIZE-1)<<2;            /* for pseudorandom lookup */
  17.   int count;                           /* count through the results in rsl[] */
  18.   int rsl[];                                /* the results given to the user */
  19.   private int mem[];                                   /* the internal state */
  20.   private int a;                                              /* accumulator */
  21.   private int b;                                          /* the last result */
  22.   private int c;              /* counter, guarantees cycle is at least 2^^40 */
  23.  
  24.  
  25.   /* no seed, equivalent to randinit(ctx,FALSE) in C */
  26.   Rand() {
  27.     mem = new int[SIZE];
  28.     rsl = new int[SIZE];
  29.     Init(false);
  30.   }
  31.  
  32.   /* equivalent to randinit(ctx, TRUE) after putting seed in randctx in C */
  33.   Rand(int seed[]) {
  34.     mem = new int[SIZE];
  35.     rsl = new int[SIZE];
  36.     for (int i=0; i<seed.length; ++i) {
  37.       rsl[i] = seed[i];
  38.     }
  39.     Init(true);
  40.   }
  41.  
  42.  
  43.   /* Generate 256 results.  This is a fast (not small) implementation. */
  44.   public final void Isaac() {
  45.     int i, j, x, y;
  46.  
  47.     b += ++c;
  48.     for (i=0, j=SIZE/2; i<SIZE/2;) {
  49.       x = mem[i];
  50.       a ^= a<<13;
  51.       a += mem[j++];
  52.       mem[i] = y = mem[(x&MASK)>>2] + a + b;
  53.       rsl[i++] = b = mem[((y>>SIZEL)&MASK)>>2] + x;
  54.  
  55.       x = mem[i];
  56.       a ^= a>>>6;
  57.       a += mem[j++];
  58.       mem[i] = y = mem[(x&MASK)>>2] + a + b;
  59.       rsl[i++] = b = mem[((y>>SIZEL)&MASK)>>2] + x;
  60.  
  61.       x = mem[i];
  62.       a ^= a<<2;
  63.       a += mem[j++];
  64.       mem[i] = y = mem[(x&MASK)>>2] + a + b;
  65.       rsl[i++] = b = mem[((y>>SIZEL)&MASK)>>2] + x;
  66.  
  67.       x = mem[i];
  68.       a ^= a>>>16;
  69.       a += mem[j++];
  70.       mem[i] = y = mem[(x&MASK)>>2] + a + b;
  71.       rsl[i++] = b = mem[((y>>SIZEL)&MASK)>>2] + x;
  72.     }
  73.  
  74.     for (j=0; j<SIZE/2;) {
  75.       x = mem[i];
  76.       a ^= a<<13;
  77.       a += mem[j++];
  78.       mem[i] = y = mem[(x&MASK)>>2] + a + b;
  79.       rsl[i++] = b = mem[((y>>SIZEL)&MASK)>>2] + x;
  80.  
  81.       x = mem[i];
  82.       a ^= a>>>6;
  83.       a += mem[j++];
  84.       mem[i] = y = mem[(x&MASK)>>2] + a + b;
  85.       rsl[i++] = b = mem[((y>>SIZEL)&MASK)>>2] + x;
  86.  
  87.       x = mem[i];
  88.       a ^= a<<2;
  89.       a += mem[j++];
  90.       mem[i] = y = mem[(x&MASK)>>2] + a + b;
  91.       rsl[i++] = b = mem[((y>>SIZEL)&MASK)>>2] + x;
  92.  
  93.       x = mem[i];
  94.       a ^= a>>>16;
  95.       a += mem[j++];
  96.       mem[i] = y = mem[(x&MASK)>>2] + a + b;
  97.       rsl[i++] = b = mem[((y>>SIZEL)&MASK)>>2] + x;
  98.     }
  99.   }
  100.  
  101.  
  102.   /* initialize, or reinitialize, this instance of rand */
  103.   public final void Init(boolean flag) {
  104.     int i;
  105.     int a,b,c,d,e,f,g,h;
  106.     a=b=c=d=e=f=g=h=0x9e3779b9;                        /* the golden ratio */
  107.  
  108.     for (i=0; i<4; ++i) {
  109.       a^=b<<11;  d+=a; b+=c;
  110.       b^=c>>>2;  e+=b; c+=d;
  111.       c^=d<<8;   f+=c; d+=e;
  112.       d^=e>>>16; g+=d; e+=f;
  113.       e^=f<<10;  h+=e; f+=g;
  114.       f^=g>>>4;  a+=f; g+=h;
  115.       g^=h<<8;   b+=g; h+=a;
  116.       h^=a>>>9;  c+=h; a+=b;
  117.     }
  118.  
  119.     for (i=0; i<SIZE; i+=8) {              /* fill in mem[] with messy stuff */
  120.       if (flag) {
  121.     a+=rsl[i  ]; b+=rsl[i+1]; c+=rsl[i+2]; d+=rsl[i+3];
  122.     e+=rsl[i+4]; f+=rsl[i+5]; g+=rsl[i+6]; h+=rsl[i+7];
  123.       }
  124.       a^=b<<11;  d+=a; b+=c;
  125.       b^=c>>>2;  e+=b; c+=d;
  126.       c^=d<<8;   f+=c; d+=e;
  127.       d^=e>>>16; g+=d; e+=f;
  128.       e^=f<<10;  h+=e; f+=g;
  129.       f^=g>>>4;  a+=f; g+=h;
  130.       g^=h<<8;   b+=g; h+=a;
  131.       h^=a>>>9;  c+=h; a+=b;
  132.       mem[i  ]=a; mem[i+1]=b; mem[i+2]=c; mem[i+3]=d;
  133.       mem[i+4]=e; mem[i+5]=f; mem[i+6]=g; mem[i+7]=h;
  134.     }
  135.  
  136.     if (flag) {           /* second pass makes all of seed affect all of mem */
  137.       for (i=0; i<SIZE; i+=8) {
  138.     a+=mem[i  ]; b+=mem[i+1]; c+=mem[i+2]; d+=mem[i+3];
  139.     e+=mem[i+4]; f+=mem[i+5]; g+=mem[i+6]; h+=mem[i+7];
  140.     a^=b<<11;  d+=a; b+=c;
  141.     b^=c>>>2;  e+=b; c+=d;
  142.     c^=d<<8;   f+=c; d+=e;
  143.     d^=e>>>16; g+=d; e+=f;
  144.     e^=f<<10;  h+=e; f+=g;
  145.     f^=g>>>4;  a+=f; g+=h;
  146.     g^=h<<8;   b+=g; h+=a;
  147.     h^=a>>>9;  c+=h; a+=b;
  148.     mem[i  ]=a; mem[i+1]=b; mem[i+2]=c; mem[i+3]=d;
  149.     mem[i+4]=e; mem[i+5]=f; mem[i+6]=g; mem[i+7]=h;
  150.       }
  151.     }
  152.  
  153.     Isaac();
  154.     count = SIZE;
  155.   }
  156.  
  157.  
  158.   /* Call rand.val() to get a random value */
  159.   public final int  val() {
  160.     if (0 == count--) {
  161.       Isaac();
  162.       count = SIZE-1;
  163.     }
  164.     return rsl[count];
  165.   }
  166.  
  167.   public static void main(String[] args) {
  168.     int  seed[] = new int[256];
  169.     Rand x = new Rand(seed);
  170.     for (int i=0; i<2; ++i) {
  171.       x.Isaac();
  172.       for (int j=0; j<Rand.SIZE; ++j) {
  173.     String z = Integer.toHexString(x.rsl[j]);
  174.     while (z.length() < 8) z = "0"+z;
  175.     System.out.print(z);
  176.         if ((j&7)==7) System.out.println("");
  177.       }
  178.     }
  179.   }
  180. }
  181.  
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement