您如何随机分配某些数字?

时间:2019-03-16 18:20:52

标签: javascript html css

我知道如何随机分配1到100之类的数字,但不能确定某些数字。就像我只想要1、15等一样。我环顾四周,而且太混乱了。有没有更简单的方法?谢谢!

function getRandom() {
return Math.random()1, 15, 30, 45;
}

所有代码(它有一个动画,我希望在动画之后的框中出现一个随机(一定)数字

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<!DOCTYPE html>
<html>
<head></head>
<title>The Wheel!</title>
<body bgcolor="orange">
<head>
    <style>
        .wheel {
            width: 200px;
            height: 100px;
            left: 600px;
            top: 300px;
            background: green;
            position: relative;
        }

        .animated-wheel {
            -webkit-animation: myfirst4s 2;
            -webkit-animation-direction: alternate;
            animation: myfirst 4s 2;
            animation-direction: alternate;
        }

        @-webkit-keyframes myfirst {
            0% {background: green; left: 600px; top: 300px;}
            33% {background: black; left: 600px; top: 0px;}
            66% {background: red; left: 600px; top: 650px;}
            100% {background: black; left: 600px; top: 0px;}
        }

        @keyframes myfirst {
            0% {background: green; left: 600px; top: 300px;}
            33% {background: green; left: 600px; top: 300px;}
            66% {background: black; left: 600px; top: 0px;}
            100% {background: red; left: 600px; top: 650px;}
        }
    </style>      
</head>
    <div class="wheel"></div>
    <button onclick="startbtn()">Start</button>
    <button onclick="refresh()">Reset</button>
    <button onclick="getRandom()">Number</button>
    <p id="getRandom()"></p>

<script>
startbtn = function() {
$('.wheel').addClass('animated-wheel');
}
function refresh() {
    location.reload();
};
function getRandom() {
var values = [1, 15, 30, 45];
return values[Math.floor(Math.random() * values.length)];
}
</script>


</body>
</html>

2 个答案:

答案 0 :(得分:2)

您可以将数字移动到数组中并返回带有随机索引的选择。

要在动画后获取一个值,您可以推迟带有超时的数字绘制,然后将该值添加到标签中。

function startbtn () {
    $('.wheel').addClass('animated-wheel');
    setTimeout(() => document.getElementById('random').innerHTML = getRandom(), 8000);
}

function refresh() {
    location.reload();
}

function getRandom() {
    var values = [1, 15, 30, 45];
    return values[Math.floor(Math.random() * values.length)];
}
.wheel { width: 200px; height: 100px; left: 600px; top: 300px; background: green;  position: relative; }
.animated-wheel { -webkit-animation: myfirst4s 2; -webkit-animation-direction: alternate; animation: myfirst 4s 2; animation-direction: alternate; }
@-webkit-keyframes myfirst { 0% { background: green; left: 600px; top: 300px; } 33% { background: black; left: 600px; top: 0px; } 66% { background: red; left: 600px; top: 650px; } 100% { background: black; left: 600px; top: 0px; } }
@keyframes myfirst { 0% { background: green; left: 600px; top: 300px; } 33% { background: green; left: 600px; top: 300px; } 66% { background: black; left: 600px; top: 0px; } 100% { background: red; left: 600px; top: 650px; } }
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<body bgcolor="orange">
    <div class="wheel"></div>
    <button onclick="startbtn()">Start</button>
    <button onclick="refresh()">Reset</button>
    <button onclick="getRandom()">Number</button>
    <p id="random"></p>
</body>

答案 1 :(得分:1)

您可以在array中输入数字,然后随机访问index

function getRandom() {
 let nums = [1, 15, 30, 45]
 return nums[Math.floor(Math.random()*nums.length)]
}

console.log(getRandom())
console.log(getRandom())
console.log(getRandom())