public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Block <panicoosha@sprintmail.com>
To: help-gcc@gnu.org
Subject: Re: gcc newbie: rand()
Date: Fri, 31 Dec 1999 22:24:00 -0000	[thread overview]
Message-ID: <38541F30.31514836@sprintmail.com> (raw)
Message-ID: <19991231222400.ZdHAc_wikC01mJwVlHDpb_hz-qNzC11VMZtmHei95vg@z> (raw)
In-Reply-To: <3850EBFA.BE9086DC@singnet.com.sg>

Joachim,

Firstly, don't assume that because it's in a book it's correct! Some books
are definitely better than others.

The best book on C (IMHO) is The C Programming Language by K&R, and on p252
of that book the ANSI C standard prototype for rand() is given...

int rand (void)
rand returns a pseudo-random integer in the range 0 to RAND_MAX, which is at
least 32767.

The book you have does not give the best way for getting a specific random
number. Your book's version only works if RAND_MAX is less than 32768, which
it isn't necessarily. I think your version will work if you replace the
magic number 32768 with RAND_MAX.

If you use modulus operator, however, you can create a more elegant formula.
This is the idiom that most C programmers use as far as I'm aware:

int result;
result = (rand () % 6) + 1;

...That should do it!

I'm also pasting an example program that demonstrates one way to get a
random number. This program compiles fine with mingw32 gcc. Here's the way I
compiled it:

    gcc -luser32 -o getrand.exe getrand.c

If you're using gcc in linux or if you're using djgpp, you can omit the
"-luser32" and it should work fine.

/***-----
|
| getrand.c
|
| Demonstrates how to generate a random
| number between 1 and a given value.
|
| NOTE: In real use, you would not want
| to call srand() before every call to
| rand(), because if you call rand()
| quickly in succession, it will keep
| returning the same results. Instead,
| move the call to srand to the beginning
| of your program and only call it once.
|
| It works in this program, but only
| because getrand() is called a single
| time.
|
|
-----***/


/*** preprocessor commands ----------------------------------***/

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


/*** global variables ---------------------------------------***/


/*** function prototypes ------------------------------------***/

int getrand (int toprange);


/*** program functions --------------------------------------***/

/*>>>_____ FUNCTION: getrand _____ */

 /***-----
 |
 | get a random # between
 | 1 and toprange
 |
 -----***/

int getrand (toprange)
{
 srand (time (NULL));
 return (rand () % toprange) + 1;
}


/*>>>_____ FUNCTION: main _____ */

 /***-----
 |
 | get toprange from
 | command line, get
 | and display a random
 | number between 1 &
 | toprange
 |
 -----***/

int main (int argc, char *argv [])
{
 int toprange = 0;

 if (argc == 2)
  toprange = atoi (argv [1]);
 if (! toprange)
 {
  printf ("\nProper usage:\n\n"
    "%s #\n"
    "Where # is positive number that "
    "represents the top value for "
    "the returned random number.",
    argv [0]);
  return 1;
 }
 printf ("\n\nRandom number between 1 & %d:\t%d",
  toprange,
  getrand (toprange));
 return 0;
}



  parent reply	other threads:[~1999-12-31 22:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-10  4:04 Joachim Bauernberger
1999-12-10  4:40 ` Jan Dvorak
1999-12-31 22:24   ` Jan Dvorak
1999-12-10 22:18 ` Martin Kahlert
1999-12-31 22:24   ` Martin Kahlert
1999-12-12 14:41 ` Patrick Block [this message]
1999-12-31 22:24   ` Patrick Block
1999-12-12 15:29 ` Rick Dearman
1999-12-31 22:24   ` Rick Dearman
1999-12-12 15:35 ` Rick Dearman
1999-12-31 22:24   ` Rick Dearman
1999-12-31 22:24 ` Joachim Bauernberger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=38541F30.31514836@sprintmail.com \
    --to=panicoosha@sprintmail.com \
    --cc=help-gcc@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).