rand() ... works ??

Jeffrey Mogul mogul at wrl.dec.com
Tue Feb 5 13:31:58 AEST 1991


In article <josevela.665174097 at academ01> josevela at academ01.mty.itesm.mx (Jose Angel Vela Avila) writes:
>  I have been trying the rand() function, but seems don't work...
>
>  I'm on a Vax 6310 with Ultrix 3.0, rand() returns me values too much hi..
>
>  Is there a trick ?

The rand() function is defined as returning an integer in the range
between 0 and (2^31)-1, inclusive.  Since the upper end of the range
is the largest possible (signed) integer, there is no possible return
value that is "too high".

If what you mean is that you don't WANT values larger than some
value of your choosing ... the usual trick is to do

myrand(x)
int x;
{
	return (rand() % x);
}

which returns an integer between 0 and x-1, inclusive.

But you really should be using the random() function, rather
than rand().  Using rand() this way will produce non-random results.
See the manual page for random(3).

-Jeff



More information about the Comp.unix.ultrix mailing list