1到4之间的随机数不是另一个随机数 [英] A random number between 1 and 4 that's not another random number

查看:195
本文介绍了1到4之间的随机数不是另一个随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  var a1 = Math.floor(Math.random() * 4 + 1); (a2 = 0; a2!= a1&& a2!= 0; a2 = Math.floor(Math.random()* 4 + 1)

// Answer2
){
alert(a2);



$ b我试图让a2 4,但不等于a1。



怎么了?
在此先感谢!!!

编辑:

谢谢大家的帮助!
这里是我的最终结果:

  var a1,a2; 
a1 = Math.floor(Math.random()* 4 + 1);
a2 = a1;

while(a2 == a1){
a2 = Math.floor(Math.random()* 3 + 1);
alert(a2);


解决方案

?初始化 a2 = a1 然后:

  while(a2 == a1 ){
a2 = Math.floor(Math.random()* 4 + 1);
}


For some reason the following code doesn't work.

var a1 = Math.floor(Math.random()*4+1); 

//Answer2
for(a2 = 0; a2 != a1 && a2 != 0; a2 = Math.floor(Math.random()*4+1)){
    alert(a2);
}

I'm trying to get "a2" to be a int from 1-4 but not equivalent to "a1".

What's wrong? Thanks in advance!!!

EDIT:

Thank you all for your help! Here's my final result:

var a1, a2;
a1 = Math.floor(Math.random()*4+1); 
a2 = a1;

while(a2 == a1){
        a2 = Math.floor(Math.random() * 3 + 1);
        alert(a2);
}

解决方案

How about a while loop instead? Initialise a2 = a1 and then:

while(a2 == a1) {
    a2 = Math.floor(Math.random() * 4 + 1);
}

这篇关于1到4之间的随机数不是另一个随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆