# How to use Math.random()?

Hi.

There are 10 items on the page.
Need to every 5 seconds randomname element assigned to the class.
Did everything, only can't combine with setInterval 2 of its functions.

``````function getRandom() {
return Math.random();
}

function random() {
if (getRandom() < 0.25) {
} else if (getRandom() > 0.25 && getRandom() < 0.5) {
} else if (getRandom() > 0.5 && getRandom() < 0.75) {
} else () {
}
}``````

Tell me how to do?
July 2nd 19 at 13:53
July 2nd 19 at 13:55
Not long ago I created a module in the gist.
Can adapt this module, but in General I can solve this problem like this:
``````import { Interval } from './path/to/time/';
import \$ from 'path/to/jquery';

let interval = new Interval();

interval.start(5000, function() {
\$('[class^=element]').removeClass('active');
});

function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}``````
July 2nd 19 at 13:57
``````setInterval(function() {
\$('.element-1, .element-2, .element-3, .element-4').removeClass('active');
\$('.element-' + (Math.floor(Math.random() * (4 - 1 + 1)) + 1)).addClass('active');
}, 5000);``````
July 2nd 19 at 13:59
You have a `getRandom()` is invoked multiple times and all the time the value is different.
So `getRandom() < 0.5 || getRandom() >= 0.5`. The first call will, for example, `0.6`and the second, for example, `0.4`.
To fix, you need to save the value in a variable:

UPD Missed
``````function getRandom() {
return Math.random();
}

function random() {
var rand = getRandom();
if (rand < 0.25) {