Logo Search packages:      
Sourcecode: csound version File versions  Download package

PUBLIC void csoundSeedRandMT ( CsoundRandMTState *  p,
const uint32_t *  initKey,
uint32_t  keyLength 
)

Initialise Mersenne Twister (MT19937) random number generator, using 'keyLength' unsigned 32 bit values from 'initKey' as seed. If the array is NULL, the length parameter is used for seeding.

Definition at line 115 of file random.c.

References csoundSeedRandMT().

Referenced by csoundSeedRandMT().

{
    int       i, j, k;
    uint32_t  x;

    /* if array is NULL, use length parameter as simple 32 bit seed */
    x = (initKey == NULL ? keyLength : (uint32_t) 19650218);
    p->mt[0] = x;
    for (i = 1; i < N; i++) {
      /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
      /* In the previous versions, MSBs of the seed affect   */
      /* only MSBs of the array mt[].                        */
      /* 2002/01/09 modified by Makoto Matsumoto             */
      x = ((uint32_t) 1812433253 * (x ^ (x >> 30)) + (uint32_t) i);
      p->mt[i] = x;
    }
    p->mti = N;
    if (initKey == NULL)
      return;
    i = 0; j = 0;
    k = (N > (int) keyLength ? N : (int) keyLength);
    for ( ; k; k--) {
      x = p->mt[i++];
      p->mt[i] = (p->mt[i] ^ ((x ^ (x >> 30)) * (uint32_t) 1664525))
                 + initKey[j] + (uint32_t) j;   /* non linear */
      if (i == (N - 1)) {
        p->mt[0] = p->mt[N - 1];
        i = 0;
      }
      if (++j >= (int) keyLength)
        j = 0;
    }
    for (k = (N - 1); k; k--) {
      x = p->mt[i++];
      p->mt[i] = (p->mt[i] ^ ((x ^ (x >> 30)) * (uint32_t) 1566083941))
                 - (uint32_t) i;                /* non linear */
      if (i == (N - 1)) {
        p->mt[0] = p->mt[N - 1];
        i = 0;
      }
    }
    /* MSB is 1; assuring non-zero initial array */
    p->mt[0] = (uint32_t) 0x80000000U;
}


Generated by  Doxygen 1.6.0   Back to index