简体   繁体   中英

Need to generate random numbers between two values ignoring the given values

I need to generate random numbers using Math.random() between two given values but ignoring on this comparation the two given values or just one depending the case.

Those are the values to do:

Between -7 and 0, excluding 0.
Between 0 and 4, both of the values excluded.
Between 4 and 5, excluding 4.

I've tried with the first case the following code:

double fv = (-7) + Math.random() * (0 -7);
System.out.println(fv);

But it's giving me weird resoults like -13.00123.

It's for a degree excercise so I have to keep it simple.

Any ideas?

Generate a number between -7 and 0 excluding 0.

      double w = (Math.random() - 1) * 7;

When the random number = 0, the required value will be -7. But the required value but will never be 0 since Math.random() will never be 1.

For the others

      // between 0 and 4 both values excluded.
      double r = Math.random();
      double x = (-3 * r); // -3 < x <= 0
      double xx = r + 3; // 3 <= xx < 4
      x += xx; // 0 < x < 4

      // between 4 and 5 excluding 4
      double w = (-1 * Math.random()); // -1 < w <= 0
      w += 5; // 4 < w <= 5

The correct formula for random numbers in a range is Min + (Math.random() * (Max - Min)) (including lower bound and excluding upper bound).
For your numbers the correct way would be

double fv = (-7) + Math.random() * (0 - -7);

Since you used only one - in the formula the Min value was assumed to be 7 and not -7 which leads to wrong results.

double fv = (-7) + Math.random() * 7;
System.out.println(fv);

creates a random double between (and including) 0.0 and 1.0 (excluding the 1.0 itself). Multiplying by 7 makes a hypothetical range of 0 to 6.99[...]9. Subtracting by 7 makes a the range of the random number from -7 to 0.00[...]01 You can use this information to deduct the formulas for your desired random numbers in a similar way.

You can set-up an enum to determine inclusivity.

import java.util.concurrent.ThreadLocalRandom;

public class RandomUtil {
    private enum Inclusivity { INCLUSIVE, EXCLUSIVE; }

    public static double randomDouble(double minValue, double maxValue) {
        return randomDouble(minValue, maxValue, Inclusivity.INCLUSIVE, Inclusivity.EXCLUSIVE);
    }

    public static double randomDouble(double minValue, double maxValue, Inclusivity minInclusivity, Inclusivity maxInclusivity) {
        if (minInclusivity == Inclusivity.EXCLUSIVE) {
            minValue += 1;
        }

        if (maxInclusivity == Inclusivity.INCLUSIVE) {
            maxValue += 1;
        }

        return ThreadLocalRandom.current().nextDouble(minValue, maxValue);
    }

    public static void main(String[] args) {
        System.out.println(randomDouble(-7, 0, Inclusivity.INCLUSIVE, Inclusivity.EXCLUSIVE)); // default
        System.out.println(randomDouble( 0, 4, Inclusivity.EXCLUSIVE, Inclusivity.EXCLUSIVE));
        System.out.println(randomDouble( 4, 5, Inclusivity.EXCLUSIVE, Inclusivity.INCLUSIVE));
    }
}

Try this

Random random = new Random();
random.ints(1, -7, 0).forEach(System.out::println);

Last value in ints() method will be excluded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM