so the title is self explanatory, I'm trying to figure out a way to increase a certain amount of values automatically and repeatedly with a different random number between 0.01 - 0.05 in an interval of 1 second ( all values should be increased all at once every 1 second ), using either JavaScript or jQuery. I don't have any existing code as I'm trying to figure this out.
Asked
Active
Viewed 168 times
-2
-
Please show us what you have tried yourself in order to create this – Carsten Løvbo Andersen Sep 27 '18 at 09:40
-
Welcome to Stack Overflow! Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic), ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask), and more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Rory McCrossan Sep 27 '18 at 09:41
-
Then figure it out in pieces. Find out how to generate random numbers in the range you want, find how to define the value you want changed, find how to automate it (almost certainly would be automatic by virtue of being code), and then how to repeat it. Hint, the last two can be be trivially combined in JS if you already have a way of automatic incrementing. – VLAZ Sep 27 '18 at 09:42
2 Answers
1
Here's what you can do - this changes a
and b
with a random number every second:
var a = 1;
var b = 1;
setInterval(function() {
var random = Math.random() * 0.05;
a += random;
b += random;
}, 1000);

Jack Bashford
- 43,180
- 11
- 50
- 79
1
What you should have done is break it down into pieces, try it out your yourself,research ,provide whatever broken code you wrote.
Here are some helpful resources : setInterval()
Generate random number between a range
Here is the probable code you are looking for :
var arr = [1,2,3,4,5];
setInterval(myFunc,1000);
function myFunc(){
var i;
for(i=0;i<arr.length;i++){
arr[i] = arr[i] + (Math.random() * (0.05-0.01) +0.01);
}
console.log(arr);
}

Loner
- 166
- 1
- 4
- 10