delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2000/06/08/19:50:52

Date: Thu, 8 Jun 2000 19:49:14 -0400
Message-Id: <200006082349.TAA30773@envy.delorie.com>
From: DJ Delorie <dj AT delorie DOT com>
To: djgpp-workers AT delorie DOT com
Subject: rand() comparison
Reply-To: djgpp-workers AT delorie DOT com

Could those of you with random number generator testers compare these
generators?  The first is from DJGPP, and the other two are from
newlib.

#define RAND_MAX 0x7fffffff

unsigned 
rand1()
{
  static unsigned long long next = 1;
  next = next * 6364136223846793005LL + 1;
  return (int)((next >> 21) & RAND_MAX);
}

unsigned
rand2()
{
  long k;
  static long s = 1;
  if (s == 0)
    s = 0x12345987;
  k = s / 127773;
  s = 16807 * (s - k * 127773) - 2836 * k;
  if (s < 0)
    s += 2147483647;
  return (int)(s & RAND_MAX);
}

unsigned
rand3()
{
  static unsigned int next = 1;
  return ((next = next * 1103515245 + 12345 ) & RAND_MAX );
}

main()
{
  int i;
  for (i=0; i<20; i++)
    printf("%15d %15d %15d\n", rand1(), rand2(), rand3());
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019