简体   繁体   English

使用Math.random()生成偶数1-4?

[英]Generate even numbers 1-4 using Math.random()?

I would like to generate the numbers 1-4 (whole integers) using Math.random. 我想使用Math.random生成数字1-4(整数)。 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(); Math.random(); = something between 0-1 as a double? =介于0-1之间的两倍?

I have seen some people suggest something like this: num = Math.random() * 60 + 25; 我见过有人提出这样的建议:num = Math.random()* 60 + 25; but have no idea what that does, or how it works. 但不知道它做什么或如何工作。

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? 编辑:有没有一种方法可以使数字不重复,但是每次运行程序时仍然是随机的?

int rand = (Math.random() * 4) + 1;

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.Random;

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. 类似的东西:(4 * Math.random())+ 1应该给您1-4之间。 correct me if I am wrong anyone. 如果我错了任何人,请纠正我。

  Random rand = new Random();
  System.out.println(rand.nextInt(4) + 1); // we add 1 because it starts with 0

If you really have to use Math.random you need to multiply (and add). 如果您确实必须使用Math.random ,则需要乘(加)。 It's quite basic math, 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; 因此,将其乘以X将得到一个大于或等于0.0且小于X的数字。将其强制转换为int以去除小数,并且如果您只想要偶数,则可以做几件事,最简单的可能是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). [我有点以为“偶数”实际上是“整数”,在这种情况下,您可以忽略它。]然后,如果您不希望范围为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). 一种方法是将您到目前为止生成的所有数字存储在List ,然后在生成新数字时,检查列表contains是否已经contains该数字,如果这样,则生成一个新数字,直到找到不在列表contains数字为止。 (然后,当您确实将其添加到列表中时)。 Another way could be to preload all numbers it could generate into a list and then remove a random number out of that list. 另一种方法是将它可能生成的所有数字预加载到列表中,然后从该列表中删除随机数。

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. 我不确定在很大范围内是否可以做些什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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