简体   繁体   中英

How can I generate different random numbers regularly in 4 second?

I'm trying to write a bingo game code. I want to produce 70 random numbers that change every 4 seconds.

I created a countdowntimer and created a random number in 4 seconds by defining a runnable into the finnish section in countdowntimer. But these random numbers may not be different from each other.

Handler handler;
Runnable run;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Random random = new Random();
    int i = random.nextInt(70);

    Random random2 = new Random();
    int z = random2.nextInt(70);
    int y =random2.nextInt(70);
    int x =random2.nextInt(70);
    int w =random2.nextInt(70);
    int q =random2.nextInt(70);
    int v =random2.nextInt(70);
    int m =random2.nextInt(70);
    int n =random2.nextInt(70);
    int l =random2.nextInt(70);


    final TextView textView3 = (TextView) findViewById(R.id.textView3);
    final TextView textView4 = (TextView) findViewById(R.id.textView4);
    final TextView textView5 = (TextView) findViewById(R.id.textView5);
    final TextView textView6 = (TextView) findViewById(R.id.textView6);
    final TextView textView7 = (TextView) findViewById(R.id.textView7);
    final TextView textView8 = (TextView) findViewById(R.id.textView8);
    final TextView textView9 = (TextView) findViewById(R.id.textView9);
    final TextView textView10 = (TextView) findViewById(R.id.textView10);



    final TextView textView2 = (TextView) findViewById(R.id.textView2);
    textView2.setText("Lucky number: " + i);

    final int a = Integer.parseInt(textView3.getText().toString());
    final int b = Integer.parseInt(textView4.getText().toString());
    final int c = Integer.parseInt(textView5.getText().toString());
    final int d = Integer.parseInt(textView6.getText().toString());
    final int e = Integer.parseInt(textView7.getText().toString());
    final int f = Integer.parseInt(textView8.getText().toString());
    final int g = Integer.parseInt(textView9.getText().toString());
    final int h = Integer.parseInt(textView10.getText().toString());

    textView9.setText("" + z);
    textView10.setText(" "+ y);
    textView8.setText(" "+ x);
    textView7.setText(" "+ w);
    textView6.setText(" "+ q);
    textView5.setText(" "+ l);
    textView4.setText(" "+ m);
    textView3.setText(" "+ n);


          CountDownTimer ct0 = new CountDownTimer(60000, 1000) {


        @Override
        public void onTick(long millisUntilFinished) {


            TextView textView = (TextView) findViewById(R.id.textView);
            textView.setText("Remaining time: " + millisUntilFinished / 1000);

        }

        @Override
        public void onFinish() {


        }
    }.start();

    CountDownTimer ct1 = new CountDownTimer(4000, 1000) {


        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {


            handler = new Handler();
            run = new Runnable() {
                @Override
                public void run() {

                    int[] numbers = new int[70];
                    Random random = new Random();
                    int i = random.nextInt(70);

                    TextView textView2 = (TextView) findViewById(R.id.textView2);
                    textView2.setText("Lucky number: " + i);

                    if ( i == a) {

                        textView3.setText("ok");
                    } else if (i == b) {
                        textView4.setText("ok");
                    } else if (i == c) {

                        textView5.setText("ok");
                    } else if (i == d) {
                        textView6.setText("ok");

                    } else if (i == e) {
                        textView7.setText("ok");
                    } else if(i == f) {
                        textView8.setText("ok");
                    } else if (i == g) {
                        textView9.setText("ok");
                    } else if (i == h) {
                        textView10.setText("ok");
                    }



                    handler.postDelayed(this, 4000);

                }
            };

            handler.post(run);



            }
        }.

        start();

}

For 60 seconds, I want to create 70 different numbers every 4 seconds and match these numbers to the other 8 variables. I'd appreciate it if you could help. thanks.

I can give to you a way to do this, to avoid what you are saying :

But these random numbers may not be different from each other.

That's obvious because you do not control it.

I'd say to create aa Set<Integer> of your size that is 70, so you can fill this array doing a simple for loop.

for (int i = 0; i<70; i++) yourSetList.add(i);

Then a good way to "simulate" the bingo is shuffling the list, so you can use Collections shuffle

Then you can generate a random like this :

Random random = new Random();   
int random = random.nextInt(yourSetList.size()) + 1;

Then use random as an index

yourSetList.get(random);

Make sure you remove it from the list to avoid your initial problem

yourSetList.remove(random);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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