[slang-users] generating random characters

John E. Davis davis at space.mit.edu
Tue Feb 12 17:03:54 UTC 2008


Michael Noble <mnoble at space.mit.edu> wrote:
> private variable rseed = getpid();
> private define random()
>{
>    rseed = (rseed * 0x5DEECEDL + 0xB) & ((1 shl 30) - 1);
>    (rseed shr 6) / ((1 shl 24) * 1.0);
>}

Another one that is ok for the purposes Troy has in mind is:

  private variable Random;
  define srandom (r)
  {
     Random = r;
  }
  srandom (_time() * getpid());

  define random ()
  {
     Random = (Random*69069U + 1013904243U)&0xFFFFFFFFU;
     return Random;
  }
  define urandom ()
  {
     return random()/4294967296.0;
  }

Here, urandom() returns a random number between 0 and 1, whereas
random returns a random unsigned integer between 0 and 0xFFFFFFFF.

FWIW, the next slang release (2.1.4) will include a random number
module.  The module will feature a generator that according to the
"dieharder" battery of tests has slightly better statistical
properties than the Mersenne Twister and is about as fast.   This
module is not yet in the svn repository but will appear on the next
update.

Good luck,
--John


More information about the slang-users-l mailing list