Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2000:
[Freeciv-Dev] Re: Random generator not random at all
Home

[Freeciv-Dev] Re: Random generator not random at all

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: Random generator not random at all
From: David Pfitzner <dwp@xxxxxxxxxxxxxx>
Date: Sat, 29 Jul 2000 18:12:45 +1000 (EST)

On 18 Jul 2000 Falk Hueffner <falk.hueffner@xxxxxxxxxxxxxxxxxxxxxxxx> wrote:

> Jarda Benkovsky <pvt.benkovsk@xxxxxxxxx> writes:
> 
> > this is a sequence of results you usually get (or s.t. similar)  when
> > you call myrand(2) repeatedly:

> > It does not look good, I am afraid :(

> ARGH it uses % to reduce the space. This is about the first thing one
> lears about RNGs... To cite rand(3) on Linux:
<snip>

> I've done a few tests, and it seems the RNG gets a lot bette after a
> while, while the first few 1000 numbers are really bad (I used
> time(NULL) as seed, like freeciv does). So in any case it would be a
> good idea to "heat it up" before each use by generating 100000 random
> numbers.

> A test that reveals the low quality is:
<snip>

This patch is intended to address the above issues (and some others).

- It uses a divisor method rather than modulus, so that it samples 
  the high order bits when size is small.  Actually I've tried to 
  be a bit clever (dangerous, I know...) and now it avoid a small 
  skew when 'size' does not exactly divide MAX_UINT32+1, using a 
  "reject" method.

- Allow myrand(0), and have it mean the full range 0 to MAX_UINT32 
  (otherwise biggest available was from myrand(MAX_UINT32) which 
  gives 0 to (MAX_UINT32-1)).

- Added a test_random1() function based on Falk's method, and call
  it in civserver if you compile with TEST_RANDOM defined.

- Pre-heat the generator with 100000 goes in mysrand().  Using 
  divisor method test_random1() looks ok even without pre-heating,
  but it seems like a good idea to pre-heat anyway (eg for more
  subtle issues not revealed by test_random1().)

Regards,
-- David

Attachment: improve_myrand.diff.gz
Description: GNU Zip compressed data


[Prev in Thread] Current Thread [Next in Thread]