delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/29/19:01:16

From: myknees AT aol DOT com (Myknees)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random implementation
Date: 29 Jan 1998 23:45:21 GMT
Lines: 84
Message-ID: <19980129234501.SAA21413@ladder02.news.aol.com>
NNTP-Posting-Host: ladder02.news.aol.com
References: <34CFD500 DOT 219A AT cs DOT com>
Organization: AOL http://www.aol.com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <34CFD500 DOT 219A AT cs DOT com>, "John M. Aldrich" <fighteer AT cs DOT com> writes:
>srand() was omitted from the documentation, but srandom() was not.
>I
>noticed this, along with several other people, and there is a posted
>bug
>report correcting the docs.  In any case, the seed functions
>probably
>ought to be mentioned in the docs for rand() and random(), and I
>think
>this will be done in v2.02.

Sounds helpful.

>Your program is very strange.  The use of
>bioskey() for example, is
>totally hardware-specific and makes no sense, when
>all you really need
>is getkey() or even getch().  

You are just seeing me trying out new things.  Is pc.h (getkey()) more portable
than bios.h?

>Furthermore, calling
>srand() once for each
>call to rand() doesn't guarantee randomness; in fact it
>makes the
>results less random.  You only need to seed a RNG once per
>program.

Wow!  You are so right.  There's a test program at the bottom of this post that
shows how un-random it is to do what I was doing--reseeding before each call to
rand().  

>As a final note, DJGPP's documentation isn't designed to teach
>standard
>C; it's designed to explain the functionality of the library code.
>I
>can see how it could be made clearer in some cases, but it would
>be
>wasteful to include a complete tutorial on random numbers in the
>library
>documentation when there are entire books devoted specifically to
>the
>subject.  :-)

Fair enough, but cross references are an easy enough shortcut if there isn't
time for explanation.  And mentioning that rand() must be seeded hardly
constitutes a tutorial.

--Ed (Myknees)

Here's how I see what you were talking about:
#include        <conio.h>
#include        <stdlib.h>
#include        <time.h>
#define         NBR_ROWS        43
#define         NBR_COLS        80

int main(void)
{
    int theRandNo, rowPosition, colPosition;
    char c;
        
    cprintf("Would you like to seed the function\n\r1) At the"
            "beginning of the program\n\r");
    cprintf("2) At each loop\n\r");
    c = getch();
    
    cprintf("Hit lowercase 'x' to exit.\n\r");
    if(c != '2')
      srand(time(0));
    
    while(getch()!='x'){
      if(c == '2')
        srand(time(0));
      theRandNo = rand();
      rowPosition = theRandNo % NBR_ROWS + 1;
      colPosition = theRandNo % NBR_COLS + 1;
      gotoxy(colPosition, rowPosition);
      cprintf("%d, %d", colPosition, rowPosition);      
    }
    return 0;
}

- Raw text -


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