delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/06/01:15:44

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random functions
Date: Sat, 06 Jun 1998 00:19:33 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 43
Message-ID: <3578C355.34F1F963@cs.com>
References: <01bd9005$b3927cc0$575995c1 AT nicland> <35772E08 DOT E9E45409 AT cs DOT com> <897091015 DOT 9997 DOT 0 DOT nnrp-07 DOT 9e989aee AT news DOT demon DOT co DOT uk>
NNTP-Posting-Host: ppp127.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Dan Goodman wrote:
> 
> >ANSI-standard RNG is rand(); however, DJGPP's implementation of it lacks
> >randomness in the lower bits.  If portability is not an issue, we
> 
> This explains a lot, have you any why this is? Should I just use random()
> instead?

I don't know the exact details of how DJGPP's rand() is non-random in
certain cases; I don't understand the underlying numerical theory well
enough.  However, the DJGPP mailing list archives at
http://www.delorie.com/djgpp/mail-archives/ should allow you to look up
the discussions we've had in the past on the subject.

As for random(), as long as your program doesn't have to be portable, I
highly recommend using it instead of rand().  The only potential problem
is that random() takes more time to execute due to its complexity.  (I'm
not sure how much longer; if you are really worried you should test it
yourself.)  If you happen to have a PhD in numerical theory and want to
see exactly how random() works, feel free to download the library
sources (djlsr201.zip) and examine
src/libc/ansi/compat/stdlib/random.c.  :-)

A final note:  all pseudo-random number generators must be seeded before
being used, or they will always generate the same sequence of numbers. 
The corresponding seed function for rand() is srand(); for random() it's
srandom().  Most programmers recommend using the system clock as a seed,
as in:

#include <stdlib.h>
#include <time.h>

    srandom( (int) time( NULL ) );

hth!

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "A generation which ignores history  |
|       aka Fighteer I       | has no past--and no future."         |
|   mailto:fighteer AT cs DOT com   |                                      |
| http://www.cs.com/fighteer |                - Lazarus Long        |
---------------------------------------------------------------------

- Raw text -


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