首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果javascript中的值相同,如何再次使用math.random来获取不同的值

如果javascript中的值相同,如何再次使用math.random来获取不同的值
EN

Stack Overflow用户
提问于 2018-08-19 07:31:19
回答 2查看 309关注 0票数 0

我正在尝试获取一个不同于先前math.random值的值,因此当这些数字写入文档时,如果它们相同,则math.random将生成另一个不相同的数字。它可能需要继续生成,直到值不同为止。

代码语言:javascript
复制
function rands() {
  return Math.floor(Math.random() * 20) + 1;
}

var rand1 = rands();
var rand2 = rands();
var rand3 = rands();
var rand4 = rands();
var rand = [];

function myFunction() {
  document.getElementById("random1").value = rand1;
  document.getElementById("random2").value = rand2;
  document.getElementById("random3").value = rand3;
  document.getElementById("random4").value = rand4;

  if(rand1 == rand2 || rand1==rand3 || rand1==rand4 || rand2 == rand3 || rand2 == rand4 || rand3==rand4){
  console.log("Duplicate");

    }
}

myFunction()
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51913277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档