delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/28/16:05:39

Date: Tue, 28 Jan 1997 21:34:57 -0200
Message-Id: <1.5.4.16.19970128185422.28df1612@dmeasc.rc.ipt.br>
Mime-Version: 1.0
To: Nils-Erik =?iso-8859-1?Q?Svang=E5rd?= <"nisse"@[194.22.170.29]>,
djgpp AT delorie DOT com
From: Cesar Scarpini Rabak <csrabak AT dce03 DOT ipt DOT br>
Subject: Re: Random numbers

At 08:16 28/01/97 -0800, Nils-Erik Svang=E5rd wrote:
>I resently downloaded DJGPP and was thrilled.
>But I have some questions.
>*How do I get the random numbers within my own limit?
>	I currently use this code:
>=09
>	while(1)
>	{
>		tt=3Drandom();
>		tt=3Dtt/10000000;
>		if(tt<320)break;
>	}
>	List[loop]=3Dtt;
>
>	Nice? no I dont think so, there must several other ways to get by=20
>	this, or some command that I've missed. (random produces a random=20
>	number between 0....MAXINT, I hope that the answer wont be to go=09
> 	change in the *.h file.
>
>*Why are my compiled files so large?
>	I've compiled a nice "little" starfield I've been working on.
>	To my suprise the *.exe file was over 1.8Mb, bad news if you have=20
>	a small hardrive. Can you decrease that value?
>
>*When I use delay(10); a warning message appears, it says error in=20
>declaration int delay(...); but it's void delay(); in the help files.
>
>
>It would be nice if someone could help me.
>/nisse
>
>

This comes from the C Language=20

13.16:	How can I get random integers in a certain range?

A:The obvious way,
 =20
  	rand() % N		/* POOR */
 =20
  (which tries to return numbers from 0 to N-1) is poor, because
  the low-order bits of many random number generators are
  distressingly *non*-random.  (See question 13.18.)  A better
  method is something like
 =20
  	(int)((double)rand() / ((double)RAND_MAX + 1) * N)
 =20
  If you're worried about using floating point, you could use
 =20
  	rand() / (RAND_MAX / N + 1)
 =20
  Both methods obviously require knowing RAND_MAX (which ANSI
  #defines in <stdlib.h>), and assume that N is much less than
  RAND_MAX.
 =20
  (Note, by the way, that RAND_MAX is a *constant* telling you
  what the fixed range of the C library rand() function is.  You
  cannot set RAND_MAX to some other value, and there is no way of
  requesting that rand() return numbers in some other range.)
 =20
  If you're starting with a random number generator which returns
  floating-point values between 0 and 1, all you have to do to get
  integers from 0 to N-1 is multiply the output of that generator
  by N.
 =20
  References: K&R2 Sec. 7.8.7 p. 168; PCS Sec. 11 p. 172.

More info on this FAQ may found in this URL

http://www.eskimo.com/~scs/C-faq/top.html

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Cesar Scarpini Rabak                      E-mail: csrabak AT ipt DOT br
DME/ASC                                   Phone: 55-11-268-3522 Ext.350
IPT - Instituto de Pesquisas Tecnologicas Fax:   55-11-268-5996
Av. Prof. Almeida Prado, 532.  Sao Paulo - SP 05508-901 BRAZIL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Raw text -


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