delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/08/01:42:54

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: a randomize function for DJGPP?
Date: Thu, 06 Mar 1997 23:16:33 -0800
Organization: Two pounds of chaos and a pinch of salt
Lines: 38
Message-ID: <331FC0D1.1A26@cs.com>
References: <5flbpp$m74 AT nr1 DOT ottawa DOT istar DOT net>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp104.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Shawn Betts wrote:
> 
> I was working with rand() and random() and noticed that there aren't any
> randomize functions that I could find for DJGPP. I found srand() and
> srandom() but they require a random seed. I would like a randomized timer
> function like so many other compilers I have seen (QBASIC, TURBO PASCAL 6.0,
> TURBO C++ 3.0). Does anyone know if I've over looked something. If I haven't
> how do I make a randomizer function?

What Borland's randomize() function does is seed the RNG with the clock
value.  In DJGPP this is not automatic; instead you get to choose what
value to use as seed.  To get an effectively random seed, use this bit
of code:

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

void randomize( void )
{
    srandom( (int) time( NULL ) );
    return;
}

To get a random number from 0 to X - 1, use:

    n = random( ) % X;

Before everybody gets hysterical on me again, let me say in advance that
the GNU random() RNG, unlike rand(), is guaranteed to be random in the
lower bits, so you can use the mod operator safely.

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I      |   mailto:fighteer AT cs DOT com   |
| God's final message to His Creation: | http://www.cs.com/fighteer |
| "We apologize for the inconvenience."| Fight against proprietary  |
|                      - Douglas Adams | software - support the FSF!|
---------------------------------------------------------------------

- Raw text -


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