Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@atuttle
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atuttle/0040a8b8226bfd5054e0 to your computer and use it in GitHub Desktop.
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.
<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>
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