Skip to main content
Participant
August 19, 2023
Answered

How to generate random numbers that change by a limited value

  • August 19, 2023
  • 1 reply
  • 403 views

I'm trying to write a script to generate random numbers that only vary by a maximum amount each change. For example, if the first number is 0.76 and I want each generated number to be no more than .05 from the last so that the next number is between 0.71 and 0.81, etc.

 

The code I have now is:

var holdTime = .5;
seedRandom(Math.floor(time/holdTime), timeless = true);
random(0,10).toFixed(2)

 

So I have the numbers changing every half second to a value between 0.00 and 10.00, but  right now the value jumps too much between numbers. (It occurs to me that statistically, the number should continue to return to its original value if the upper and lower change values are equal)

This topic has been closed for replies.
Correct answer Dan Ebberts

Like this maybe:

var holdTime = .5;
seedRandom(index,true);
var curTime = 0;
var curValue = random(10);
while (curTime <= time){
  curValue += random(-.05,.05);
  curTime += holdTime;
}
curValue.toFixed(2)

1 reply

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
August 19, 2023

Like this maybe:

var holdTime = .5;
seedRandom(index,true);
var curTime = 0;
var curValue = random(10);
while (curTime <= time){
  curValue += random(-.05,.05);
  curTime += holdTime;
}
curValue.toFixed(2)
Participant
August 19, 2023

Exactly what I wanted! Thanks a ton.