我正在尝试获取数字 57 和 48、90 和 65、122 和 97 之间的随机数。
(int) (Math.random * (max-min) + 1);
这通常有效,但是当我用它来获取上述数字时
(int) (Math.random() * (57-48) + 1);
(int) (Math.random() * (90-65) + 1);
(int) (Math.random() * (122-97) + 1);
我最终得到的变量太小了。 (示例:5、14、3、15 和 8)。
我不明白为什么它不适用于这些特定数字。
最佳答案
return Math.floor(Math.random() * (57 - 48 + 1)) + 48;
return Math.floor(Math.random() * (90 - 65 + 1)) + 65;
return Math.floor(Math.random() * (122 - 97 + 1)) + 97;
这对你有用吗?
关于java - Math.random 没有给我一个我正在寻找的范围内的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47775172/