delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/24/18:53:47

Message-ID: <379A42F1.AF23A6DF@vetec.com>
Date: Sat, 24 Jul 1999 17:49:21 -0500
From: Andy Goth <andygoth AT vetec DOT com>
X-Mailer: Mozilla 4.5 [en] (Win95; I)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: -- Random question --
References: <7nd0vu$igt$1 AT zingo DOT tninet DOT se> <7nd9so$a96 AT dfw-ixnews3 DOT ix DOT netcom DOT com>
Reply-To: djgpp AT delorie DOT com

> You can use the remainder operator like this:
> 
> number = (rand() % 101) + 1;

The rand() % 101 part will return a number from 0 to 100, so if you add
one to that you get 1 to 101.  Try (rand() % 100) + 1 instead (1 to
100).
 
> This will generate a number from 1 to 100. You can also substitute rand()
> with random() which generates numbers that are more random, but random()
> isn't as portable. Don't forget to seed it at the start of your program like
> this:
> 
> srand(time(0)); /* for rand() */
> srandom(time(0)); /* for random() */

- Raw text -


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