How do I generate a random integer between min and max in Java?

后端 未结 8 1263
醉酒成梦
醉酒成梦 2020-12-13 09:15

What method returns a random int between a min and max? Or does no such method exist?

What I\'m looking for is something like this:

NAMEOFMETHOD (mi         


        
8条回答
  •  情书的邮戳
    2020-12-13 09:40

    public static int random_int(int Min, int Max)
    {
         return (int) (Math.random()*(Max-Min))+Min;
    }
    
    random_int(5, 9); // For example
    

提交回复
热议问题