delorie.com/archives/browse.cgi | search |
Message-Id: | <199803240715.JAA31075@ankara.duzen.com.tr> |
From: | "S. M. Halloran" <mitch AT duzen DOT com DOT tr> |
Organization: | User RFC 822- and 1123-compliant |
To: | "Nathan Cournia" <nac2b AT frank DOT mtsu DOT edu> |
Date: | Tue, 24 Mar 1998 09:19:27 +0200 |
MIME-Version: | 1.0 |
Subject: | Re: Random interger numbers |
CC: | djgpp AT delorie DOT com |
In-reply-to: | <01bd56dd$5d636c80$0f02000a@frank.mtsu.edu.frank.mtsu.edu> |
On 24 Mar 98 Nathan Cournia was found to have commented thusly: > 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 this one out: return (rand() % n); I think it works almost every time. Yeah, I know, it looks too simple. I have pounded my hand into my head of this and other supposedly simple problems in the past. Your approach of course would occur to any thoughtful mathematician, but engineers always seem to find ways to frustrate mathematicians. Mitch Halloran Research (Bio)chemist Duzen Laboratories Group Ankara TURKEY mitch AT duzen DOT com DOT tr other job title: Sequoia's (dob 12-20-95) daddy
webmaster | delorie software privacy |
Copyright © 2019 by DJ Delorie | Updated Jul 2019 |