Javascript: Generate random numbers with fixed mean and standard deviation -
my question:
how can create list of random numbers given mean , standard deviation (sd) in javascript?
example:
i want create list 5 random numbers in range between 1 10. resulting mean should 5 , standard deviation should 2.
what i've done far:
my idea (http://jsfiddle.net/qasdg/3/):
- make function (named "createdata") creates 5 random numbers between 1 , 10 , push them "array"
- this function should calculate mean (and sd) of 5 numbers.
- with "do-while"-loop: above function long "average becomes 5" (and "sd becomes 2"). when call do-while loop, browser crashes of course because there lot of cases when average != 5 (and sd != 2).
note: haven't add sd yet, since code isn't elegant.
i've found script site stated: "the goal of code generate collection filled 5 distributed random numbers mean of 1.0 , standard deviation of 0.5." http://rosettacode.org/wiki/random_numbers#javascript. i've tried adjust according needs adding "math.floor" , changed condition in "for"-loop (i < 5).
function randomnormal() { return math.floor(math.cos(2 * math.pi * math.random()) * math.sqrt(-2 * math.log(math.random()))) } var = [] (var i=0; < 5; i++){ a[i] = randomnormal() / 2 + 1 }
how want modify code:
i'm not quite sure whether i've understood mathematical part of code. according webpage create normal distribution (which great not necessary). need in addition is:
- where can change given mean , sd (e.g. mean = 5, sd = 2) in order create random numbers (which fulfill properties).
- how implement range between 1 , 10? (maybe use like: math.floor(math.random()*10)+1)
thanks lot!
Comments
Post a Comment