[英]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. 我想产生70个随机数字,每4秒更改一次。
I created a countdowntimer and created a random number in 4 seconds by defining a runnable into the finnish section in countdowntimer. 我通过在countdowntimer的finish部分中定义了一个runnable,创建了一个countdowntimer并在4秒内创建了一个随机数。 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. 对于60秒,我想每4秒创建70个不同的数字,并将这些数字与其他8个变量进行匹配。 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. 我想说创造出一个Set<Integer>
你的尺码是70,所以你可以填补这个数组做一个简单for
循环。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.