0

I have an array with 365 elements. Each index can be thought of as one day in the past year. The value stored at each index represents the number of times that an item was purchased on that day. array[0] is the number of times an item was purchased 365 days ago, and array[364] is the number of times an item has been purchased so far today.

Alright, so what I want to do is populate the array with random values. However, I want to simulate real-world data. If I just use random number, an item might be purchased 80 times one day, and 2 times the next, which is unrealistic. More realistic would be 80 purchases one day, then 75 the next, then 82, then 76, etc. I am wondering if there is a way I can use arithmetic mean, standard deviation, regression, correlation, or similar tools to quickly generate arrays with realistic data simulations using Javascript.

pnuts
  • 58,317
  • 11
  • 87
  • 139
jdogg
  • 268
  • 2
  • 14
  • https://en.wikipedia.org/wiki/Random_walk – Barmar Oct 06 '15 at 15:32
  • 1
    Basically, instead of picking a totally random value, you pick a random small number and add it to the previous value. – Barmar Oct 06 '15 at 15:34
  • Further to @Barmar's comment, there's an example [here](http://jsfiddle.net/Xotic750/3rfT6/) which comes from http://stackoverflow.com/questions/22058195/generating-a-smooth-random-trend-random-walk-in-javascript . – user5325596 Oct 06 '15 at 15:40

1 Answers1

1

you can use math.random and restrict the values to be within a range

var days = [];
var max = 80;
var min = 50;

for(var i=0;i<365;i++){
    days[i] = Math.floor(Math.random() * (max - min + 1)) + min;
}

alert(days);

https://jsfiddle.net/7daffjh8/16/

if the next value must be within x of the previous value, try this

var days = [80];
var max = 5;
var min = -5;

for(var i=1;i<365;i++){
    var difference = Math.floor(Math.random() * (max - min + 1)) + min;
    days[i] = days[i-1] + difference;
}

alert(days);

https://jsfiddle.net/7daffjh8/17/

stackoverfloweth
  • 6,669
  • 5
  • 38
  • 69