Java - limit number between min and max

后端 未结 7 704
耶瑟儿~
耶瑟儿~ 2020-12-06 08:53

I want to return the number as long as it falls within a limit, else return the maximum or minimum value of the limit. I can do this with a combination of Math.min

7条回答
  •  离开以前
    2020-12-06 09:30

    You do not need an external library for this, try this test case:

    public class RandomNumber {
    public static void main(String[] Args) {
        System.out.println("random = " + randomInRange(5,10));
    }
    
    public static double randomInRange(double arg1, double arg2) {
        double my_number = Math.ceil(Math.random() * (arg1 - arg2) + arg2);
        return my_number;
     }
    

    }

提交回复
热议问题