如何在Android Java中创建一个介于最小值和最大值之间的随机数,排除一些落在最小值和最大值之间的数字?
如何在Android Java中创建一个介于最小值和最大值之间的随机数,排除一些落在最小值和最大值之间的数字?
我有三个变量,每个变量都是 1-100 之间的随机数。我能够使用 math.rand() 进行随机数,但我试图确保这三个数字不匹配。我使用 while 和 if 语句做了一项工作,但我想看看是否有一行命令可以执行此操作,以便我可以将其放在活动类下,使其成为公共变量。在该区域(活动)中,我无法使用 while 和 if 语句,由于 void 或其他原因,我只能在 onCreate 中使用。
预先感谢您的帮助,并将投票支持任何有助于找到有关此信息的帮助或想法。
How to create in Android Java a random number between min and max excluding some numbers that fall between min and max.?
I have three vars, each is a random number between 1-100. I was able to do the random number using the math.rand() but I am trying to make sure that the three numbers do not match. I did a work around using while and if statements, but I was looking to see if there is a one line of command to do this, so that I can put it right under th activity class so that it's public var. and in that area (activity) I can't use while and if statements, I only can in onCreate due to the void or something.
Thank you in advance for your help, and will vote for any help or idea that will lead to finding any info about this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不需要“一行命令”。您可以使用构造函数或初始化块来执行您想要的任何操作。
当然,您也可以将这些东西放入构造函数中——事实上,这几乎就是 Java 看到它时所做的事情。即使像
public int x = 1;
这样的初始化内容实际上也会进入构造函数。将其放入构造函数中的唯一缺点是,您需要将其添加到每个构造函数中,而如果您使用初始值设定项(块或单个语句),Java 会为您完成此操作。There's no need for "one line of command". You can use the constructor or initializer blocks to do just about anything you want.
Of course, you can put this stuff into a constructor too -- in fact, that's pretty much what Java does with it when it sees it. Even the initialization stuff like
public int x = 1;
actually goes into a constructor. The only drawback with putting it in a constructor is, you need to add it to each constructor, while if you use initializers (blocks or single statements), Java does it for you.所有这些软件程序本质上都是伪随机数生成器,而不是真正的随机数生成器。因此,在某些角落/偏远情况下,您最终可能会获得连续生成的相同随机数。因此,除了放置 while 循环/if else 块来处理此类极端情况之外,没有其他选择。所以我认为你已经在做正确的理想事情,而 while 循环/if else 块不应该成为问题,因为随机数冲突将是罕见的情况。
All these software programs are essentially pseudo random number generators and not true random number generators. So in some corner/remote cases it may happen that you end up with getting same random number generated consecutively. So there is not other option than putting while loop/if else block to handle such corner cases. So what I think is that you are already doing the correct ideal thing and while loop/if else blocks should not be problem as random number collisions will be rare case scenarios.
它必须通过一些条件表达式(if、while、do-while 等)来完成,因为必须“检查”生成的数字以查看它们是否相等。
如果变量是静态的,则应在静态构造子句中初始化它们:
如果变量不是静态的,则可以在活动的 OnCreate 方法中初始化它们。如果在调用活动之前使用它们或将它们标记为最终状态,则应在活动的构造函数中初始化它们。
It must be done by some conditional expressions (if, while, do-while, etc.), since the generated numbers must be 'checked' to see if they are equal.
If the variables are static, you should initialize them in the static construction clause:
If they are not static, you may initialize them in the activity's OnCreate method. In the case of using them before the activity is invoked or they are marked as final, you should initialize them in the activity's constructor.
这是我的 AndroidActivity 代码,它可以工作。
This my AndroidActivity code, and it works.
如果你想获得 3 个不同的随机整数,而不需要任何“if”或“while”,你可以使用 getThreeRandomInt 方法。
(我为测试创建了帮助类“Dots”,这不是必要的)
import static org.junit.Assert.*;
导入 java.util.Random;
导入 org.junit.Test;
公共类 getThreeRandomInt
{
公共静态类点
{
公共整数a;
公共 int b;
公共 int c;
}
If you want to get 3 different random ints without any 'if' or 'while' you could use getThreeRandomInt method.
(I create help class 'Dots' for tests, it's not nessesery)
import static org.junit.Assert.*;
import java.util.Random;
import org.junit.Test;
public class getThreeRandomInt
{
public static class Dots
{
public int a;
public int b;
public int c;
}