delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/05/06:28:51

Sender: crough45 AT amc DOT de
Message-Id: <97Jun5.122135gmt+0100.16656@internet01.amc.de>
Date: Thu, 5 Jun 1997 11:25:15 +0100
From: Chris Croughton <crough45 AT amc DOT de>
Mime-Version: 1.0
To: deef AT pobox DOT oleane DOT com
Cc: djgpp AT delorie DOT com
Subject: Re: Random numbers/George

Francois Charton wrote:

> We say that random() is better than rand() becauses there are tests which 
> rand() is known to fail that random() passes. But, once more, these 
> correspond to very subtle "non randomness", in 99.99% of the cases, 
> including all game programming, you can consider rand() and random() to 
> be as good as each other.

Actually, for the most common use (rand() % n) they are not equally as 
good.  The problem with rand() is that the bottom bits are not at all
random, in fact they are a fairly short sequence especially if 'n' is
a power of 2 (i.e. extracting the bottom bits).  I believe it just 
alternates even and odd numbers, for instance.

With a better RNG like random() there is no difference between the 
randomness of any bits compared to any other.  For instance, nothing
is gained by doing (random()>>8)%16 compared to random()%16.

> As for the property you quote: each number having the same frequency, 
> this is verified on average by almost all generators (including rand() 
> and random()). 

And, of course, by a repeating sequence within the range.  For instance:

int randNoInRange(int range)
{
  static int r = 0;
  return (r++ % range);
}

fulfils that criterion.  It fails several other tests, though...

Chris

- Raw text -


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