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 badges3 Answers
Reset to default 5Something 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
}
本文标签:
版权声明:本文标题:javascript - How can I generate random numbers for demo charts that roughly trend up and to the right? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741919977a2404954.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论