0

I was wondering if I am able to set the accuracy of the random double numbers that I generate.

Random generator = new Random();
double randomIndex = generator.nextDouble()*10000;

That produces the random numbers within 10000. How am I able to set the accuracy to 4?

jsh6303
  • 2,010
  • 3
  • 24
  • 50

5 Answers5

2
         Random generator = new Random();
         double randomIndex = generator.nextDouble()*10000;            
         randomIndex=Math.floor(randomIndex * 10000) / 10000;//this is the trick
Trying
  • 14,004
  • 9
  • 70
  • 110
  • @Ingo It *does* work, it's just that your original question was unclear. – Jason C Nov 17 '13 at 22:34
  • 1
    All solutions, including my own, have the error that a double simply does not have "decimal positions", hence you can end up with numbers that print with more (or less) decimals. – Ingo Nov 17 '13 at 22:46
  • @Ingo You *did* cover that by explicitly mentioning it in your answer, which is why yours is a good one in that respect. :) I also mentioned storing as integers, and converting on display, for exact precision in mine. – Jason C Nov 17 '13 at 22:51
2

A couple of things. First, you mean "precision", not "accuracy". Second, you need to clarify why you want to do this, because a correct answer will depend on it.

If all you want to do is display the numbers with that precision, it is a formatting issue. You can use, e.g. System.out.printf("%.4f", value) or String.format().

If you are trying to to generate numbers with that precision, you could approximate by doing something like (rounding left out for simplicity):

double value = (int)(generateor.nextDouble() * 10000.0) / 10000.0;

Or if you want your range to be 0-10000 instead of 0-1:

double value = (int)(generateor.nextDouble() * 100000000.0) / 10000.0;

Due to the way floating-point numbers are stored, that will not be exact, but perhaps it is close enough for your purposes. If you need exact, you would want to store as integers, e.g.:

int value = (int)(generator.nextDouble() * 10000.0);

Then you can operate on that internally, and display as:

System.out.printf("%.4f", value / 10000.0);

Adjust multiplication factor above if you meant you wanted your range to be 0-10000.

If you are merely trying to generate a number in [0, 10000), you can use Random.nextInt(int) with a range specified, or simply cast the value to an int as above (optionally rounding).

Jason C
  • 38,729
  • 14
  • 126
  • 182
1

If you want 4 digits after the decimal mark you can simply do the following:

Random generator = new Random();
double randomIndex = Math.floor(generator.nextDouble()*10000 * 10000) /10000;
Basim Khajwal
  • 946
  • 6
  • 6
1
Random generator = new Random();
double randomIndex = Double.parseDouble(new DecimalFormat("##.####")
                .format(generator.nextDouble() * 10000));
Sandhu Santhakumar
  • 1,678
  • 1
  • 13
  • 24
1

Simply

double result = generator.nextLong() / 10000.0;

Note, hoewever, that you can never be sure that the number has exactly 4 decimals, whenever you hit a number that is not representable in a double.

Anyway, the requirement is silly, because a double simply does not have decimal positions. Hence, to request 4 of them makes no sense.

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • It is worth noting that the theoretical range of this is -922337203685477.5808 to 922337203685477.5807; actual range and distribution susceptible to limits of `nextLong()` and internal representation of a `double`. – Jason C Nov 17 '13 at 22:41