Last active
August 29, 2015 14:07
-
-
Save atuttle/0040a8b8226bfd5054e0 to your computer and use it in GitHub Desktop.
Generating random numbers with a trend to make data appear more realistic in some scenarios. 50% chance for a direction change.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
//to initialize to a random number instead of 50, set last to "" | |
last = 50; | |
for(var i = 1; i <= 100; i++){ | |
last = trendingRandom( 0, 100, 12, last ); | |
writeOutput( "#last#<br/>" ); | |
} | |
</cfscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function trendingRandom(rangeMin, rangeMax, volatility = 1, last = ""){ | |
/* | |
volatility is an integer between 1 and 100 that indicates how wild | |
the swings should be. 12 is a good place to start. | |
*/ | |
if ( last == "" ){ | |
return randRange( rangeMin, rangeMax ); | |
} | |
var direction = (randRange(0,1) == 0) ? -1 : 1; | |
volatility = randRange(1, volatility) / 100; | |
var diff = direction * volatility * last; | |
var computed = round(last + diff); | |
return Max( rangeMin, Min(rangeMax, computed) ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment