X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f In-Reply-To: <200709050405.l854521N014918@delorie.com> Subject: Re: random() : What am I doing wrong? To: djgpp AT delorie DOT com X-Mailer: Lotus Notes Release 7.0.2 September 26, 2006 Message-ID: From: Gordon DOT Schumacher AT seagate DOT com Date: Wed, 5 Sep 2007 09:01:37 -0600 X-MIMETrack: Serialize by Router on SV-GW1/Seagate Internet(Release 7.0.1 HF29|March 07, 2006) at 09/05/2007 08:01:41 AM MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII X-Proofpoint-FWRule: outbound2 X-Proofpoint-Virus-Version: vendor=fsecure engine=4.65.5502:2.3.11,1.2.37,4.0.164 definitions=2007-09-05_06:2007-09-05,2007-09-05,2007-09-05 signatures=0 Errors-To: nobody AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk DJ Delorie wrote on Tue, 4 Sep 2007 15:48:27 -0400: # > What about if I want to generate numbers in the range of 1 through 50? # > How do I specify my range? # # Take the result and "% 50". That depends if you care about equidistribution; if you do, you'd be better off doing something more or less like this: unsigned int raw = random(); unsigned int scaled = (unsigned int) (raw * (50 / (double) UINT_MAX)); At the cost of some performance, of course. If you do a modulo, you effectively "fold" the original random number space back on itself - and the numbers will be weighted towards the first "n" (where n is the remainder of the division). The code above actually scales the number so you don't have that problem. It's not a problem for most cases... it just depends on what characteristics you need out of your output.