On Oct 2, Bob Showalter said:

>> -----Original Message-----
>> From: eric [mailto:[EMAIL PROTECTED]]
>> 
>> show how to creat a random number inthe following range:
>> 5 to 15
>> -10 to 20
>> 
>> the documentation for rand and srand are not too useful.
>
>Well, they're extremely useful in telling what those routines
>do, so I don't understand your complaint.
>
>Anyway, since rand(x) generates a random number in the range
>0 <= n < x, it should be trivial to map that to the range
>a <= n < b, right?

Yes, the rand() function returns some value greater than or equal to 0 and
less than the value you pass it (which defaults to 1).  If you want an
integer in the range from 0 to $N - 1, you would do:

  $val = int rand($N);

If you then want an interger in the range from 1 to $N, you'd add 1 to the
RETURNED value:

  $val = 1 + int rand($N);

Use this to figure out how to shift the ranges to get numbers from 5 to 15
and -10 to 20.

  0     <= rand(N)     < N
  0 + A <= rand(N) + A < N + A

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to