如何在Android Java中创建一个介于最小值和最大值之间的随机数,排除一些落在最小值和最大值之间的数字?

发布于 2024-12-08 21:34:23 字数 309 浏览 0 评论 0原文

如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

妄想挽回 2024-12-15 21:34:23

不需要“一行命令”。您可以使用构造函数或初始化块来执行您想要的任何操作。

public class blah
{
    public int a, b, c;

    // this runs when the object is created
    {
        Random r = new Random();
        a = r.nextInt(100) + 1;
        do { b = r.nextInt(100) + 1; } while (a == b);
        do { c = r.nextInt(100) + 1; } while (a == c || b == c);
    }
}

当然,您也可以将这些东西放入构造函数中——事实上,这几乎就是 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.

public class blah
{
    public int a, b, c;

    // this runs when the object is created
    {
        Random r = new Random();
        a = r.nextInt(100) + 1;
        do { b = r.nextInt(100) + 1; } while (a == b);
        do { c = r.nextInt(100) + 1; } while (a == c || b == c);
    }
}

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.

凡间太子 2024-12-15 21:34:23

所有这些软件程序本质上都是伪随机数生成器,而不是真正的随机数生成器。因此,在某些角落/偏远情况下,您最终可能会获得连续生成的相同随机数。因此,除了放置 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.

茶色山野 2024-12-15 21:34:23

它必须通过一些条件表达式(if、while、do-while 等)来完成,因为必须“检查”生成的数字以查看它们是否相等。

如果变量是静态的,则应在静态构造子句中初始化它们:

public class MyClass {
    public static final int a, b, c;

    static {
        // generate the random numbers here //
    }
}

如果变量不是静态的,则可以在活动的 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:

public class MyClass {
    public static final int a, b, c;

    static {
        // generate the random numbers here //
    }
}

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.

摇划花蜜的午后 2024-12-15 21:34:23

这是我的 AndroidActivity 代码,它可以工作。

package com.androidbook.droid1;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
public class DroidActivity3 extends Activity
{
public static class GetThreeRandomIntClass
{
    public static class Dots 
    { 
        public int a; 
        public int b; 
        public int c; 

        public Dots()
        {
                a = 0;
                b = 0;
                c = 0;
        }

        @Override
        public String toString()
        {
            return "[" + a + "]" + "[" + b + "]" + "[" + c + "]";
        }
    }

    public static void getThreeRandomInt(Dots d)
    {
        int[] arr = new int[100];
        Random r = new Random();
        for(int i=0; i<arr.length ; i++){
            arr[i] = i + 1;
        }
        int randInt = r.nextInt(100);
        d.a = arr[randInt];
        for(int i=randInt; i<arr.length - 1 ; i++){
            arr[i] = arr[i + 1];
        }
        randInt = r.nextInt(99);
        d.b = arr[randInt];
        for(int i=randInt; i<arr.length - 2 ; i++){
            arr[i] = arr[i + 1];
        }
        randInt = r.nextInt(98);
        d.c = arr[randInt];
    }
}




@Override
protected void onCreate(Bundle savedInstanceState)
{

    this.setContentView(R.layout.third);

    GetThreeRandomIntClass.Dots d = new GetThreeRandomIntClass.Dots();
    GetThreeRandomIntClass.getThreeRandomInt(d);

    TextView textView1 = (TextView) this.findViewById(R.id.textView1);
    textView1.setText(String.valueOf(d.a));

    TextView textView2 = (TextView) this.findViewById(R.id.textView2);
    textView2.setText(String.valueOf(d.b));

    TextView textView3 = (TextView) this.findViewById(R.id.textView3);
    textView3.setText(String.valueOf(d.c));

    super.onCreate(savedInstanceState);
}}

This my AndroidActivity code, and it works.

package com.androidbook.droid1;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
public class DroidActivity3 extends Activity
{
public static class GetThreeRandomIntClass
{
    public static class Dots 
    { 
        public int a; 
        public int b; 
        public int c; 

        public Dots()
        {
                a = 0;
                b = 0;
                c = 0;
        }

        @Override
        public String toString()
        {
            return "[" + a + "]" + "[" + b + "]" + "[" + c + "]";
        }
    }

    public static void getThreeRandomInt(Dots d)
    {
        int[] arr = new int[100];
        Random r = new Random();
        for(int i=0; i<arr.length ; i++){
            arr[i] = i + 1;
        }
        int randInt = r.nextInt(100);
        d.a = arr[randInt];
        for(int i=randInt; i<arr.length - 1 ; i++){
            arr[i] = arr[i + 1];
        }
        randInt = r.nextInt(99);
        d.b = arr[randInt];
        for(int i=randInt; i<arr.length - 2 ; i++){
            arr[i] = arr[i + 1];
        }
        randInt = r.nextInt(98);
        d.c = arr[randInt];
    }
}




@Override
protected void onCreate(Bundle savedInstanceState)
{

    this.setContentView(R.layout.third);

    GetThreeRandomIntClass.Dots d = new GetThreeRandomIntClass.Dots();
    GetThreeRandomIntClass.getThreeRandomInt(d);

    TextView textView1 = (TextView) this.findViewById(R.id.textView1);
    textView1.setText(String.valueOf(d.a));

    TextView textView2 = (TextView) this.findViewById(R.id.textView2);
    textView2.setText(String.valueOf(d.b));

    TextView textView3 = (TextView) this.findViewById(R.id.textView3);
    textView3.setText(String.valueOf(d.c));

    super.onCreate(savedInstanceState);
}}
与酒说心事 2024-12-15 21:34:23

如果你想获得 3 个不同的随机整数,而不需要任何“if”或“while”,你可以使用 getThreeRandomInt 方法。
(我为测试创建了帮助类“Dots”,这不是必要的)

package Random;

import static org.junit.Assert.*;

导入 java.util.Random;

导入 org.junit.Test;

公共类 getThreeRandomInt
{
公共静态类点
{
公共整数a;
公共 int b;
公共 int c;

    public Dots()
    {
        a = 0;
        b = 0;
        c = 0;
    }

    @Override
    public String toString()
    {
        return "[" + a + "]" + "[" + b + "]" + "[" + c + "]";
    }
}

public static void getThreeRandomInt(Dots d)
{
    int[] arr = new int[100];
    Random r = new Random();
    for(int i=0; i<arr.length ; i++){
        arr[i] = i + 1;
    }
    int randInt = r.nextInt(100);
    d.a = arr[randInt];
    for(int i=randInt; i<arr.length - 1 ; i++){
        arr[i] = arr[i + 1];
    }
    randInt = r.nextInt(99);
    d.b = arr[randInt];
    for(int i=randInt; i<arr.length - 2 ; i++){
        arr[i] = arr[i + 1];
    }
    randInt = r.nextInt(98);
    d.c = arr[randInt];
}

@Test
public void test()
{
    Dots d = new Dots();

    for(int i=0; i<300000; i++){
        getThreeRandomInt(d);
        assertFalse( d.a == d.b || d.a == d.c || d.b == d.c);
        //System.out.println(d.toString());
    }
}

}

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)

package Random;

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;

    public Dots()
    {
        a = 0;
        b = 0;
        c = 0;
    }

    @Override
    public String toString()
    {
        return "[" + a + "]" + "[" + b + "]" + "[" + c + "]";
    }
}

public static void getThreeRandomInt(Dots d)
{
    int[] arr = new int[100];
    Random r = new Random();
    for(int i=0; i<arr.length ; i++){
        arr[i] = i + 1;
    }
    int randInt = r.nextInt(100);
    d.a = arr[randInt];
    for(int i=randInt; i<arr.length - 1 ; i++){
        arr[i] = arr[i + 1];
    }
    randInt = r.nextInt(99);
    d.b = arr[randInt];
    for(int i=randInt; i<arr.length - 2 ; i++){
        arr[i] = arr[i + 1];
    }
    randInt = r.nextInt(98);
    d.c = arr[randInt];
}

@Test
public void test()
{
    Dots d = new Dots();

    for(int i=0; i<300000; i++){
        getThreeRandomInt(d);
        assertFalse( d.a == d.b || d.a == d.c || d.b == d.c);
        //System.out.println(d.toString());
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文