-2

I know how to randomize numbers like 1 to 100 but not certain numbers. Like I only want like 1, 15 , etc. I was looking around and its too confusing. Is there a easier way? Thank you!

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

ALL THE CODE ( It has an animation and I want a random (certain) number to appear in the box after the animation

<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>
  • 1
    Possible duplicate of [Getting a random value from a JavaScript array](https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array) and [Get random item from JavaScript array](https://stackoverflow.com/questions/5915096) – adiga Mar 16 '19 at 18:23
  • Hello! You will find the answer here: https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array If that is still too hard to understand let us know the exact part of it that is troublesome and someone can help. – Ray Toal Mar 16 '19 at 18:23

2 Answers2

1

You could move the numbers in an array and return a pick with a random index.

For gettiing a value after the animation, you could defer the drawing of the numer with a timeout and add the value later to a tag.

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>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can have numbers in array and than access index randomly

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())
Code Maniac
  • 37,143
  • 5
  • 39
  • 60