1. Member
Join Date
Jul 2013
Posts
14
Rep Power
0

## Generating random numbers.

How can I generate a random number between a range of numbers using a number generated by Math.random ()? I know in order to get a number between 0 and n, I simply have to multiply Math.random () by n, but I can't figure out how to do it between a range of m and n. Any ideas? It's an exercise in a textbook I'm learning from:

Exercise 10.1 Write a method called randomDouble that takes two doubles, low
and high, and that returns a random double x so that low  x < high.

Exercise 10.2 Write a method called randomInt that takes two arguments, low and
high, and that returns a random integer between low and high (including both).

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,642
Rep Power
9

## Re: Generating random numbers.

Try this on some paper. Write down a bunch of random numbers between 0 and n. Then add some constant m to each one. See what happens.

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•