0

The Java api has a method on how to generate integers between 0(inclusive) and n(exclusive), but does not have a method to generate random numbers of double values, given a certain range, say 2.0 to 3.0. How can this be done?


 double x = 0.7 + Double.valueOf(Math.random()*(9.0-0.7)).intValue(); 

The result its just few numbers of type .7

FranXh
  • 4,481
  • 20
  • 59
  • 78
  • Exact duplicate: http://stackoverflow.com/questions/363681/java-generating-random-number-in-a-range – Doug Stephen Apr 24 '12 at 02:15
  • The one you showed me, I have already seen it, but it talks a=only about INTEGRES. I need doubles – FranXh Apr 24 '12 at 02:18
  • It's very easily generalized. Just cast the integer and multiply it by a randomly generated double. – Doug Stephen Apr 24 '12 at 02:20
  • Hey sir I tried to and I cannot figure it out. Is this forum for asking question or for closing them? – FranXh Apr 24 '12 at 02:21
  • @user1181847 Yes, that question does only talk about integers. But the same logic applies to doubles as well. – Jeffrey Apr 24 '12 at 02:23
  • We've given you a suitable answer and your question shows no code showing your attempt. – Hovercraft Full Of Eels Apr 24 '12 at 02:23
  • Here is my code showing my attempt: double x = 0.7 + Double.valueOf(Math.random()*(9.0-0.7)).intValue(); And this just generates 4 or 5 nr of type .7 nothing else. It might be a really "easy" or "logical" question, but if I am asking for help means that I need help. And if you think so please do not comment at all – FranXh Apr 24 '12 at 02:26
  • Edit your question and put your code attempt in there, not in a comment because code loses its formatting in comments and is impossibe to read. And it's not whether you're asking for help or not, it's that you were given a solution and just threw up your hands and gave up without showing that you tried to use the solution given. – Hovercraft Full Of Eels Apr 24 '12 at 02:27

3 Answers3

2

You're very close, but the intValue() call is unnecessary and the Double.valueOf( String ) call shouldn't even compile.

public double random( double min, double max )
{
  double diff = max - min;
  return min + Math.random( ) * diff;
}
ulmangt
  • 5,343
  • 3
  • 23
  • 36
  • Thank you, this works I think. I was expecting to get doubles with only one digit after the decimal point, but I guess this would work too. – FranXh Apr 24 '12 at 02:34
  • 1
    @user1181847 Since Java `doubles` are binary numbers, a value like `10.3` can't be represented exactly, so it really doesn't make sense to ask for a `double` with one digit after the decimal point. However, if you want to print your number rounded to one decimal point, try: `System.out.printf( "%.1f%n", 10.3 )`. Also check out the [DecimalFormat](http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html) class. – ulmangt Apr 24 '12 at 02:45
  • 1
    @user1181847: if what you want is random numbers with step of 0.1, then use the int solution for numbers 10 * your desired numbers (i.e., if you want between 7 and 9, solve for 70 to 90). Then take the result and divide by 10.0. – Hovercraft Full Of Eels Apr 24 '12 at 02:59
1

The marked answer excludes the maximum (nextDouble's maximum is 0.9). As a side note I would like to present my answer including the maximum.

public double randomDouble(double min, double max) {
    double divider = 1.1111111111111111111111111111111; 
    double num = min + (random.nextDouble() * ((max - min) * divider)); 
    return num2; 
}
Bram
  • 4,533
  • 6
  • 29
  • 41
0

Have you tried the Random class instead? http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Random.html There´re methods for float, double...