XY model. TEA random number generator


T It delay fps

XY model. Monte-Carlo simulations. This script uses random number generator based on the Tiny Encryption Algorithm made by Tomi Aarnio.

uint2 rand(uint2 seed, int iterations) {
  uint sum = 0;
  uint delta = 0x9E3779B9;
  uint k[4] = { 0xA341316C, 0xC8013EA4, 0xAD90777D, 0x7E95761E };
  for (int j=0; j < iterations; j++) {
    sum += delta;
    seed.x += ((seed.y << 4) + k[0]) & (seed.y + sum) & ((seed.y >> 5) + k[1]);
    seed.y += ((seed.x << 4) + k[2]) & (seed.x + sum) & ((seed.x >> 5) + k[3]);
  }
  return seed;
}
It generates a pair of 32-bit random numbers from the given pair of 32-bit seed values seed and t (see below)
  int ix = get_global_id(0),  iy = get_global_id(1),  t = ix + n*iy;
  uint2 rnd = (seed, seed << 3);
  rnd.x += t + (t << 11) + (t << 19);
  rnd.y += t + (t << 9) + (t << 21);
  rnd = rand(rnd, iter);
The number of iterations iter can be adjusted depending on the required degree of randomness: 16 iterations is typically enough for any use.

For iter = 8 the script is ~3 times slower then XY model with the linear congruential random number generator.


WebCL Demos     updated 11 Sep 2011