user5367186
user5367186

Reputation: 67

Generate a double random in increments of specific number

 private static double getSliceQuantityinRange(double min, double max) {
    double range = max - min;
    double scaled = randomGenerator.nextDouble() * range;
    double shifted = scaled + min;
    return Double.valueOf((df.format(shifted)));
  }

min and max will be in millions. This gives a random number between a range. but what I want is the random number in steps of 100 thousand ( 100000). How to achieve that?

Upvotes: 0

Views: 524

Answers (1)

rsutormin
rsutormin

Reputation: 1649

Here is an example:

public static void main(String[] args) {
    System.out.println(getSliceQuantityinRange(35000000, 52000000, 100000));
}

private static double getSliceQuantityinRange(double min, double max, double step) {
    int range = (int)((max - min) / step);
    double scaled = randomGenerator.nextInt(range) * step;
    double shifted = scaled + min;
    return Double.valueOf((df.format(shifted)));
}

Upvotes: 1

Related Questions