www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/08/29/13:23:47

Xref: news2.mv.net comp.os.msdos.djgpp:8020
From: pvkeer AT vub DOT ac DOT be (Vankeerberghen Pieter)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: random() and rand() question
Date: 29 Aug 1996 14:58:48 GMT
Organization: Brussels Free Universities VUB/ULB
Lines: 30
Message-ID: <504b78$4ku@rc1.vub.ac.be>
References: <Pine DOT SUN DOT 3 DOT 91 DOT 960829095429 DOT 24833A-100000-100000-100000 AT sun630>
NNTP-Posting-Host: orca.vub.ac.be
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Francisco Javier Pena Pareja (i32japef AT sun630 DOT uco DOT es) wrote:
: On Wed, 28 Aug 1996, J.J. Pierson wrote:

: > Silly question... How would I go about getting a random number from 
say 1
: > to a specified number?
: > 

: It's quite simple. Let's suppose you want to get a random number from 0 
: to 255. Then:

: 	rand()%256

: will do that. From 1 to 255?

: 	rand()%255 + 1


I think this is a bad technique for numerical stuff. 
The lower bits of random generators are not reaaly random. One better
scales the rand() as in

	low + (rand()/(max_rand)) * (high - low +1)

with low and high describing the interval and max_rand, the maximal
value for the generator in casu.

For games the modulo operator might be sufficient, but certainly not
for simulations.

- Raw text -


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