The moose likes Beginning Java and the fly likes Random  number problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random  number problem" Watch "Random  number problem" New topic

Random number problem

Terence hiu
Ranch Hand

Joined: Oct 21, 2012
Posts: 36
Hi, is there a way of generating a random number of type double from -500 to 500 using the import random class?
Steve Fahlbusch

Joined: Sep 18, 2000
Posts: 596


assuming you are using java 7 (if not change 7 to the version you are using) and google 'java Random api 7'.

After reading the API you should find that......

First of all the class is java.util.Random.

Second there is a method that will return a type you want, but the range needs to be changed.

And how do you change a numeric range? Well to make the range wider or small you could multiply by some factor.

And to change the base (i.e.: the lowest part of the range) to some number you could add (or subtract) some number.

Hope this helps


Oh by the way, the answer to the question you asked was a simple - yes.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45277
If you use java.util.Random, you have a method which makes that whole procedure simpler.
It is sorta covered in the JavaRanch Style Guide.
subject: Random number problem
It's not a secret anymore!