www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/08/04:07:23

From: gerhardvanm AT transnet DOT co DOT za (Gerhard)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: random numbers
Date: Fri, 08 Jan 1999 08:51:44 GMT
Organization: SPOORNET
Lines: 32
Message-ID: <774h4s$64i$1@hermes.is.co.za>
References: <36940389 DOT 31E6 AT bellsouth DOT net>
NNTP-Posting-Host: gauntleto.transnet.co.za
X-Newsreader: Forte Free Agent 1.0.82
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

tim park <sethpark AT bellsouth DOT net> wrote:

>Ok, I know this doesn't have all that much to do with djgpp, but say I
>wanted a random number between 0 and 20. How would I do that? Would you
>please just write me just some code (in c please!!) that would do that?

>Sad air


Here is an example from PROGRAMMING WITH C++ by John Hubbard
(ISBN 0-07-114328-9), a very good text book on C++ to get you going.

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

main()
{
	unsigned seed = time(NULL);		// assigns the number of seconds

						// elapsed in 20th century to seed
	srand(seed);				// seed the randomiser
	for (int i = 0; i < 32; i++)
		cout << 1 + rand()%6 << " ";	// simulate dice - random number would
						// be from 0 to 5 hence the 1 +
	cout << endl;
}

Hope it helps

Gerhard

- Raw text -


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