delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/24/22:45:48

From: root AT delorie DOT com (Demandred)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random interger numbers
Date: Wed, 25 Mar 1998 00:11:53 GMT
Organization: Cerberus Software
Message-ID: <35184ba3.3329073@news.compusmart.ab.ca>
References: <01bd56dd$5d636c80$0f02000a AT frank DOT mtsu DOT edu DOT frank DOT mtsu DOT edu>
NNTP-Posting-Host: remote225.compusmart.ab.ca
Lines: 21
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On 24 Mar 1998 04:31:32 GMT, "Nathan Cournia" <nac2b AT frank DOT mtsu DOT edu> was heard
to ramble:

>	How do I produce a random interger number between 0 and n.  Right now I'm
>using the following code:
>
>	return (((int)((double)rand()/(double)RAND_MAX))*n);
>
>	Unfortunately this equation always produces the result of 0.  What I need
>is a function that will act as a (n) sided dice.  Thanks for the help.
>

Try:

return (random()%n)+1;


--
Demandred, Lord of the 32-bit Registers
http://www.nnetis.ca/~matt/dogfight.html
mail to: demandrd 'at' compusmart 'dot' ab 'dot' ca

- Raw text -


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