random numbers in c

John A. Weeks III john at newave.UUCP
Sun Apr 28 10:04:26 AEST 1991


In <9104261022.aa28844 at Bonnie.ics.uci.edu> sto at Bonnie.ICS.UCI.EDU writes:
> does anyone know how to generate random numbers in c from X to
> Y , where  X and Y can be postive or negative ?

Assuming that you have a procedure that returns random numbers between
0 and 1, try using n = X + ( random() * ( Y - X ) ).  You might want
to compute the range somewhere else, and then take the absolute value
of it (in case X > Y).

-john-

-- 
=============================================================================
John A. Weeks III               (612) 942-6969             john at newave.mn.org
NeWave Communications                       ...uunet!tcnet!wd0gol!newave!john



More information about the Comp.lang.c mailing list