This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes generating random number in range Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "generating random number in range" Watch "generating random number in range" New topic
Author

generating random number in range

Bilal Ali
Ranch Hand

Joined: Jun 14, 2007
Posts: 66
I am trying to write a very simple program that will generate a random number from 65 to 90(betwen 65 and 90) , I dont want to use the Random class or nextInt() method , i simply want to achieve this by using the Math.random() , can some one please help or give some suggestion.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
random() returns a number between 0.0 and 1.0. You will need to scale this, and shift the result to an appropriate range. So the way to do this would be:

double scale = ???;
double shift = ???;

double rand = Math.random() * scale + shift;


What would appropriate values for scale and shift be?

As an aside, using the Random class might be simpler, since it can return an integer, and has a method that takes care of the scaling built in.
[ February 22, 2008: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generating random number in range
 
Similar Threads
operator cannot be applied to ints.
Create Random Number
QuickSort Question
converting ascii number to capital letter
Random Characters???