Ouch! Floating point *and* a divide for a random number?

I take the points on board for simulations where maximum entropy
is the main goal...but for a *game* on a 16 bit (okay, okay, for the
pedantic a 16/32 bit) CPU that doesn't have a FPU?

Speed has to take priority...you could do something like this:

SysRandom()>>(32-bitsyouwant);

So, for 1 random bit, 0-1:
short result = SysRandom()>>31;

So, for 2 random bit, 0-3:
short result = SysRandom()>>30;

So, for 3 random bit, 0-7:
short result = SysRandom()>>29;


(Don't forget, google is your friend!  You can do better than SysRandom!)

Heck, when you're coding a game with very specific requirements, you can
_usually_ do better than many system functions speed-wise, because you
don't have to be nearly as general-case as they are.

-Ken

On Mon, 2003-01-27 at 17:26, Marco Pantaleoni wrote:
> On Tue, Jan 28, 2003 at 12:57:04AM +0800, Geoffrey wrote:
> >=20
> > If I mod it with 5, then the result will be 0, 1, 2, 3, 4, 0, 1, 2, 3,
> > 4, 0, 1 (each have same probability to gen), then the chance of the "0"
> > and "1" return is 3/12 and the other is 2/12. The probaility will not b=
e
> > same for each value!
> >=20
> > I am now try to give a random result of a dice, it is not fair that som=
e
> > number have more probaility to return!
>=20
> I will cite section 3 of rand function man page (glibc):
>=20
>        In Numerical Recipes in C: The Art of Scientific Computing
>        (William  H.  Press, Brian P. Flannery, Saul A. Teukolsky,
>        William T.  Vetterling;  New  York:  Cambridge  University
>        Press,  1992 (2nd ed, p. 277)), the following comments are
>        made:
>               "If you want to generate a random integer between 1
>               and 10, you should always do it by using high-order
>               bits, as in
>=20
>                      j=3D1+(int) (10.0*rand()/(RAND_MAX+1.0));
>=20
>               and never by anything resembling
>=20
>                      j=3D1+(rand() % 10);
>=20
>               (which uses lower-order bits)."
>=20
>        Random-number generation is a complex topic.  The  Numeri=AD
>        cal  Recipes  in  C book (see reference above) provides an
>        excellent discussion of practical random-number generation
>        issues in Chapter 7 (Random Numbers).
>=20
>        For  a  more theoretical discussion which also covers many
>        practical issues in depth, please see  Chapter  3  (Random
>        Numbers) in Donald E. Knuth's The Art of Computer Program=AD
>        ming, volume 2 (Seminumerical Algorithms), 2nd ed.;  Read=AD
>        ing,  Massachusetts:  Addison-Wesley  Publishing  Company,
>        1981.
>=20
> Hope this helps.
> Ciao,
> Marco
>=20
> --=20
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Marco Pantaleoni                                  [EMAIL PROTECTED]
> Padova, Italy                                              [EMAIL PROTECTED]
> elastiC language developer                   http://www.elasticworld.org
>=20
> --=20
> For information on using the Palm Developer Forums, or to unsubscribe, pl=
ease see http://www.palmos.com/dev/support/forums/
>=20







-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to