slang-users mailing list

[2008 Date Index] [2008 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] generating random characters


Michael Noble <mnoble@xxxxxxxxxxxxx> 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



[2008 date index] [2008 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]