delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/23/19:07:02

Message-ID: <35169C99.3B53@pobox.oleane.com>
Date: Mon, 23 Mar 1998 18:32:09 +0100
From: Francois Charton <deef AT pobox DOT oleane DOT com>
Organization: CCMSA
MIME-Version: 1.0
To: Guillaume BETOUS <g DOT betous AT hol DOT fr>
CC: djgpp AT delorie DOT com
Subject: Re: real random numbers
References: <351634A2 DOT 5ACD AT hol DOT fr>

Guillaume BETOUS wrote:
> 
> If anyone could tell me how to get REAL random generated numbers under
> djgpp ... and/or explain me how it works ! I wonder for a long time how
> a computer can generate random numbers
> 

The standard library for djgpp has two random number generators: rand() 
and random(). Both of them return a random integer value at each call.

Before using them, you have to "seed" them, by calling srand(some_number) 
or srandom(some_number) first. If you use twice the same seed 
(some_number), you get twice the same random series (useful for 
generating random looking maps). If you want a "true" (non replicable) 
random series, you may want to use srand(time()) as a seed. 

As far as efficiency is concerned, rand() is less random but faster than 
random(). Both of these functions are usually more than enough for all 
practical game/graphics/scientific programming (so I suggest you always 
use rand(), which is faster, portable and good enough).

If you have a "scientific" need for random numbers, that is, you want to 
prove something using random numbers, or you are doing research on topics 
where the randomness of your numbers is crucial, then you should always 
use "home made" generators, and if possible, try using several different 
generators for the same problem, to be sure that the results of your 
calculations owe as little as possible to "artefacts" from the generator 
you use...

Many algorithms are used to produce random numbers. The best known of 
them are "linear congruential generators": let a,b,c be three numbers, 
each random value Y is calculated from the previous X by the formula:
Y = aX + b (mod c)
If a,b,c are cleverly chosen, you obtain a fairly random generator (ie, 
it is very hard, knowing X, to guess where Y will fall...). 

More complicated formulae can be used to produce more random generators. 
However, a "randomly made" generator, almost never produce good random 
numbers: if you want to design your own generators, you should study some 
theory first.

IMHO, there are two good reference books on the subject: 

Numerical Recipes in C (Cambridge) has a chapter on random numbers, with 
examples in C, and a good discussion. It is not too involved 
mathematically, and is a good first read on the subject (as well as many 
others). 

The Art of Computer Programming, volume 2, seminumerical algorithms, by 
Knuth, is a very detailed treatment of the problem both from a 
mathematical and programming perpective. It also presents algorithms for 
testing generators. However, it is not easy reading, and suppose you have 
a slight interest in maths...

Hope this helps
Francois

- Raw text -


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