Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate random numbers for demo charts that roughly trend up and to the right?

I need to generate random numbers for several charts that go up and to the right.

I'm using a JavaScript charting engine so will ultimately require numbers in JSON, but I can handle the conversion if you have an easy way outside of JavaScript.

Here's a simple randomNumber generator in JavaScript:

function randomNumber(minimum, maximum){
    return Math.round( Math.random() * (maximum - minimum) + minimum);
}
console.log(randomNumber(0,100));

The above would work if min and max grew over time. Can you point me in the right direction?

Here's a JSFiddle to try out various solutions, including a handy chart: http://jsfiddle.net/9ox4wjrf/

Here's a rough example of the sorts of charts I need to build with generated data:

rough example of desired results

like image 804
Ryan Avatar asked Feb 11 '23 16:02

Ryan


1 Answers

Something like this may work:

var a = 0.05;
var b = 10; //play with these values to your liking
var y;

//loop here from 0 to whatever
y = a * x^2 + b * x * Math.random();
//or using your randomMumber function:
y = a * x^2 + randomMumber(- b * x / 2, b * x / 2);

This way the noise gets bigger when further to the right

like image 100
strah Avatar answered Feb 15 '23 11:02

strah