delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/02/19:18:42

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random Numbers
Date: Mon, 02 Feb 1998 19:03:06 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 32
Message-ID: <34D65EBA.236C@cs.com>
References: <MPG DOT f3ea776c7b2ff97989685 AT news DOT rmci DOT net>
NNTP-Posting-Host: ppp229.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Tim Elliott wrote:
> 
> How do I get a random number in DJGPP like say a random number between 1 to 100?

Read the libc docs:  "info libc alpha random"... "info libc alpha
srandom".

Remember that DJGPP != Borland; you must seed the random number
generator yourself, and random() returns a value from 0 to MAXINT that
you must clip to the desired range.  Sample code:

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

int main( void )
{
    int i;
    srandom( (int) time( NULL ) );
    for ( i = 0; i < 100; i++ )
        printf( "%3d ", random( ) % 100 + 1 );
    printf( "\n" );
    return 0;
}

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Animals can be driven crazy by pla- |
|       aka Fighteer I       | cing too many in too small a pen.    |
|   mailto:fighteer AT cs DOT com   | Homo sapiens is the only animal that |
| http://www.cs.com/fighteer | voluntarily does this to himself."   |
---------------------------------------------------------------------

- Raw text -


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