admin管理员组

文章数量:1312637

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: /

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

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/9ox4wjrf/

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

Share Improve this question edited Oct 30, 2014 at 20:58 aarosil 4,8983 gold badges29 silver badges41 bronze badges asked Oct 30, 2014 at 15:06 RyanRyan 15.3k34 gold badges115 silver badges190 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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

Define your trend without noise, as an array of growing numbers. Call that X and copy it into another array Y. Then for each point in Y add a number generated by the built-in random number generator in Math. This will add the illusion of noise.

If you want more random options, checkout random-js on GitHub. https://github./mobiusklein/random-js. It is a great library but forks are helping smooth the rough edges. Also, https://github./tmcw/simple-statistics for linear regression lines.

This will be pseudocode, but you could do something like this:

int randomRangePotential = 20; // percentage of random growth/shrinkage
int likelyhoodOfGrowth = 95; // likelyhood of datapoint being more than the previous
int numberOfDataPoints = 100; // number of data points to generate

int lastDataPointValue = 50;

for (int iterator = 0; iterator < numberOfDataPoints; iterator++)
{
    // generate random number to determine positive or negative growth 0 - 100
    // generate randomRange, a random number between 1 and randomRangePotential
    // if random number > likelyhoodOfGrowth

       // generate random number for datapoint that is (lastDataPoint = lastDataPoint * 1.randomRange)
       // add the random number to a datapoint

    // if random number < likelyhoodOfGrowth

       // generate random number for datapoint that is (lastDataPoint = lastDataPoint * -1.randomRange)
       // add the random number to a datapoint

    // lastDataPointValue = thisdatapoint

}

本文标签: