This week's book giveaway is in the JavaScript forum. We're giving away four copies of Meteor in Action and have Stephan Hochhaus & Manuel Schoebel on-line! See this thread for details.

Math.random() is a good place to start. If that doesn't suit your needs, then java.util.Random should be your next stop.

Math.random() generates a double between 0 and 1 (including 0, but not 1). As T Dahl suggested, you can multiply this value by some factor to increase your range, then shift that range by adding or subtracting.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org

In general, for any function f that returns a value between a and b, to get a value between c and d apply the following calculation: (f(x) - a) / (b - a) * (d - c) + c.

The subtraction of a gives you a value between 0 and (b - a).
The dividing by (b - a) gives you a value between 0 and 1 (provided b != a).
The multiplication gives you a value between 0 and (d - c).
The addition of c gives you a value between c and d.

In this case, a == 0 and b == 1, so you are left with Math.random() * (d - c) + c.