1

i want to draw values from two or more arrays. For example arrays 'age' which contain values 1-99 and second array 'earnings' which contain 2000-10000. If i will draw 'age' more than 30 i would like to get value from second array, but the probability that i will get more than 5000 must be for example more than 75%. How to make it possible?

Dawid Kwiatoń
  • 157
  • 2
  • 13
  • Are you asking how to access a random element in a Javascript array? See here: https://stackoverflow.com/questions/5915096/get-random-item-from-javascript-array – Lincoln Bergeson May 22 '17 at 17:17
  • Easy, make a random from 0 to 1, if its lower than 0.25 choose from 2000 to 5000 and if not choose from 5000 to 10000 – juvian May 22 '17 at 17:35
  • but i don't want only 5000-10000 when the age is over 30. I want lower values too, but with lower possibility to get lower numbers :D – Dawid Kwiatoń May 22 '17 at 17:51

1 Answers1

1

You could use a check for the age and two formulas for calcualting the value for earnings, without stepping.

condition   random range       formula       min    max
---------  -------------  ----------------  -----  -----
 <= 5000   0.00 ... 0.25  r * 12000 + 2000   2000   5000
  > 5000   0.25 ... 1.00  r *  6666 + 3333   5000  10000

Example with count for 100000 random earnings, with age > 30

function getRandomEarnings(age) {
    var r = Math.random();
    if (age > 30) {
        return r < .25 ? r * 12000 + 2000 : r * 6666.666666 + 3333.333333;
    } else {
        // add some more random values
    }
}

var count = [0, 0],
    i;

for (i = 0; i < 1e6; i++) {
    count[+(getRandomEarnings(35) > 5000)]++;
}

console.log(count);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392