开发者

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

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.


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.


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.


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.


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);
}}


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());
    }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜