Java 使用 Math.random() 生成偶数 1-4?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19262530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate even numbers 1-4 using Math.random()?
提问by
I would like to generate the numbers 1-4 (whole integers) using Math.random. I have only succeeded in getting doubles or large doubles, and cannot figure out how to set a limit on the minimum and maximum.
我想使用 Math.random 生成数字 1-4(整数)。我只成功获得了双打或大双打,无法弄清楚如何设置最小值和最大值的限制。
Math.random(); = something between 0-1 as a double?
Math.random(); = 0-1 之间的东西作为双倍?
I have seen some people suggest something like this: num = Math.random() * 60 + 25; but have no idea what that does, or how it works.
我看到有些人提出这样的建议: num = Math.random() * 60 + 25; 但不知道它有什么作用,或者它是如何工作的。
I am not sure if this is a true question, and feel free to let me know if I should delete it.
我不确定这是否是一个真实的问题,如果我应该删除它,请随时告诉我。
Edit: Is there a way to not get the numbers to repeat, yet still be random every time the program is run?
编辑:有没有办法不让数字重复,但每次程序运行时仍然是随机的?
回答by Sterling Archer
Math.Random
is redundant here, use the Random
class.
Math.Random
这里是多余的,使用Random
类。
Random rand = new Random();
rand.nextInt(4)+1; //starts at 0, so add 1
Import this class by:
通过以下方式导入此类:
import java.util.*;
or import java.util.Random;
import java.util.*;
或者 import java.util.Random;
回答by wpochron
the random number in math gives you a decimal number between zero and one. you need to tell it to be within a certain range. something like: (4*Math.random())+1 should give you between 1-4 I think. correct me if I am wrong anyone.
数学中的随机数给你一个介于零和一之间的十进制数。你需要告诉它在某个范围内。类似于: (4*Math.random())+1 应该给你 1-4 我认为。如果我错了,请纠正我。
回答by Thusitha Thilina Dayaratne
Random rand = new Random();
System.out.println(rand.nextInt(4) + 1); // we add 1 because it starts with 0
回答by LazyCubicleMonkey
int rand = (Math.random() * 4) + 1;
回答by Alowaniak
If you really have to use Math.random
you need to multiply (and add).
It's quite basic math, Math.random()
如果你真的必须使用Math.random
你需要乘(和加)。这是很基础的数学,Math.random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
返回带正号的双精度值,大于或等于 0.0 且小于 1.0。
So multiplying it with X will give a number greater than or equal to 0.0 and less than X. Cast that to an int to get rid of decimals and if you only want even numbers you can do a few things, the easiest probably being int even = (notSureIfEven >> 1) << 1;
. [I'm kind of assuming that with 'even' numbers you actually meant 'whole' numbers though, in which case you can ignore that.] Then if you don't want the range to be 0->X but Y->X you just add Y to your outcome (make sure to subtract Y from X before the multiplication or your range will be Y->X+Y).
因此,将它与 X 相乘将得到一个大于或等于 0.0 且小于 X 的数字。将其转换为 int 以去除小数,如果您只想要偶数,您可以做一些事情,最简单的可能是int even = (notSureIfEven >> 1) << 1;
. [我有点假设对于“偶数”数字,您实际上是指“整个”数字,在这种情况下,您可以忽略它。] 然后,如果您不希望范围为 0->X 但 Y-> X 您只需将 Y 添加到您的结果中(确保在乘法之前从 X 中减去 Y,否则您的范围将是 Y->X+Y)。
To not generate the same number twice you can do different things. One way is to store all the numbers you generated so far in a List
and then when you generate a new number, check if the list contains
that number already, if so generate a new one until you got one that isn't in the list (and then when you do obviously add that to the list). Another way could be to preload all numbers it could generate into a list and then remove a random number out of that list.
为了不生成相同的数字两次,您可以做不同的事情。一种方法是将您到目前为止生成的所有数字存储在 a 中List
,然后当您生成一个新数字时,检查contains
该数字是否已经列出,如果是,则生成一个新数字,直到您得到一个不在列表中的数字(和然后当您显然将其添加到列表中时)。另一种方法是将它可以生成的所有数字预加载到列表中,然后从该列表中删除一个随机数。
Both ways probably won't scale very well to really large ranges of numbers though. The first one since it might get in a very long loop trying to find a number it hadn't generated yet, the second one because you'll have to create a really large list at the start. I'm not sure if there's something you could do in the case of a really large range.
不过,这两种方式可能都不能很好地扩展到非常大的数字范围。第一个是因为它可能会进入一个很长的循环,试图找到一个它尚未生成的数字,第二个是因为您必须在开始时创建一个非常大的列表。我不确定在非常大的范围内是否可以做些什么。