I have seen posts which explains pretty much this question but they all used integer values and I honestly do not fully understand it hence this question:

I am trying to generate random numbers from the range (-1554900.101) to (52952058699.3098) in java and I was wondering if anyone could explain this to me as I really want to understand it.

My thoughts:

will this be a right approach?

1) Generate a random integer number within my specified range

2) Divide the generated number by pi to get float/double random results

Thanks in advance.

## Best Solution

Here's the idea. You want a random number in a

range, let's say`[-1.1,2.2]`

, to start with a simple example. That range has length 3.3 since`2.2 - (-1.1) = 3.3`

. Now most "random" functions return a number in the range`[0,1)`

, which has length one, so we have toscaleour random number into our desired range.Now our random number has the magnitude we want but we must shift it in the number line to be between the exact values we want. For this step, we just need to add the lower bound of the entire range to our scaled random number and we're done!

So now we can put these parts together in a single function:

Of course, this function needs some error checking for unexpected values like

`NaN`

but this answer should illustrate the general idea.