This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random number problem

 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, is there a way of generating a random number of type double from -500 to 500 using the import random class?
 
Steve Fahlbusch
Bartender
Posts: 602
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

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

-steve

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

 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use java.util.Random, you have a method which makes that whole procedure simpler.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic