所以我构建了这个网页,并在 6 个小盒子中设置了 6 个随机数的函数。每个数字都有一个盒子,有时我在两个盒子里收到相同的数字,我需要这个不相等。我需要在所有盒子上使用不同的号码。从 1 到 37。非常感谢大家!!! 这是功能以及所有的 css 和 html 设计。
function six() {
var s = document.getElementsByClassName("trim");
for (var i = 0; i < s.length; i++) {
console.log(s[i]);
s[i].innerHTML = Math.floor((Math.random() * 37) + 1);
}
}
.lotobox {
width: 550px;
height: 100px;
border: 1px solid black;
background-color: #0080ff;
color: #E52A34;
font-size: 25px;
text-align: center;
margin: auto 0;
padding-bottom: 15px;
}
.numbers {
width: 550px;
height: 530px;
border: 1px solid black;
background-color: darkcyan;
color: black;
text-align: center;
}
th {
border: 4px solid black;
width: 70px;
height: 100px;
padding: 10px 20px 10px;
background-color: gray;
color: black;
text-align: center;
font-family: vardana;
font-size: 40px;
}
td {
text-align: center;
}
#button {
width: 110px;
height: 40px;
margin: 0 auto;
}
.table1 {
margin: 0 auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Loto Aplication</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<div class="lotobox">
<h1>Loto Box</h1>
</div>
<div class="numbers">
<br><br>
<table class="table1">
<tr>
<th class="trim"></th>
<th class="trim"></th>
<th class="trim"></th>
</tr>
<tr>
<th class="trim"></th>
<th class="trim"></th>
<th class="trim"></th>
</tr>
</table>
<br>
<button id="button" onclick="six()">go!</button>
</div>
</body>
</html>
最佳答案
math.random() 函数未“播种”- 因此每次加载页面并调用函数时可能会返回相同的值,就像您展示的那样。
查看之前关于编写您自己的 javascript 随机种子生成器的 StackOverflow 线程:Seeding the random number generator in Javascript
您可以将其与评论中建议的数组/检查器功能结合使用,以确保编号唯一。
关于javascript - 随机数javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47420256/