Mazzy
Mazzy

Reputation: 14189

generate rand value belonged to two different ranges

I need to generate random values using Random object. It generates values from 0 to 1, but I want to generate random values that fall into the ranges [0,10e-7] and [10e-7,1].

Upvotes: 1

Views: 528

Answers (2)

Bohemian
Bohemian

Reputation: 425043

double max, min; 
if (Math.random() > .5) { // adjust ratio of ranges here
    min = 0;
    max = .00000001;
} else {
    min = .00000001;
    max = 1;
}
double random = Math.random() * (max - min) + min;

Upvotes: 3

Dan D.
Dan D.

Reputation: 32391

You can use the nextDouble() method of the Random class to get a number between 0 and 10e-7.

Then, in the first case, divide the number by 10e7 to get a value in the [0, 10e-7] range and add 10e-7 to the generated value if it is lower than this value for the second case.

Upvotes: 0

Related Questions