簡體   English   中英

如何在Android Java中創建一個介於最小和最大之間的隨機數,不包括介於最小和最大之間的一些數字?

[英]How to create in Android Java a random number between min and max excluding some numbers that fall between min and max.?

如何在Android Java中創建一個介於最小和最大之間的隨機數,不包括介於最小和最大之間的一些數字?

我有三個變量,每個都是1-100之間的隨機數。 我能夠使用math.rand()來做隨機數,但我試圖確保這三個數字不匹配。 我做了一個關於使用while和if語句的工作,但我想看看是否有一行命令來做這個,所以我可以把它放在活動類下面,這樣它就是public var。 並且在那個區域(活動)我不能使用while和if語句,我只能在onCreate由於空白或其他東西。

提前感謝您的幫助,並將投票選出任何有助於找到相關信息的幫助或想法。

不需要“一行命令”。 您可以使用構造函數或初始化程序塊來執行您想要的任何操作。

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會為您執行此操作。

所有這些軟件程序本質上都是偽隨機數生成器,而不是真正的隨機數生成器。 因此,在某些角落/遠程情況下,您最終可能會連續生成相同的隨機數。 因此除了使用while / if else塊來處理這樣的極端情況之外沒有其他選擇。 所以我認為你已經在做正確的理想事情,而loop / if else塊不應該是問題,因為隨機數沖突將是罕見的情況。

必須通過一些條件表達式(if,while,do-while等)來完成,因為必須“檢查”生成的數字以查看它們是否相等。

如果變量是靜態的,則應在static construction子句中初始化它們:

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

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

如果它們不是靜態的,您可以在活動的OnCreate方法中初始化它們。 如果在調用活動之前使用它們或將它們標記為final,則應在活動的構造函數中初始化它們。

如果你想獲得3個不同的隨機整數而沒有'if'或'while'你可以使用getThreeRandomInt方法。 (我為測試創建幫助類'Dots',它不是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());
    }
}

}

這是我的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);
}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM