0

I have a simple implementation question. Here is the random number function I have, and returns a random number from a given range, inclusive.

function randomNum(low, high){
     return Math.floor(Math.random() * (high - low + 1)) + low;
  }

However, I would like to have 50% chance of getting the high number, and 25% for everything else..

for example:

randomNum(1, 3)

'3' would have a 50% chance of getting a hit, while '1' and '2' will both have a hit percentage of 25%. I'm not too sure as to what changes I need to make to my function...tips would be great, Thanks

  • Must it always be 50%, or twice as likely as everything else? e.g., randomNum(1,4) the chances of 4 are 50%, or chances of 4 are 40%? – jpriebe Feb 12 '15 at 04:04
  • @jpriebe yea, the high number must have a 50% hit rate, no matter the range given. – user6623512 Feb 12 '15 at 04:06
  • 1
    So if I did `randomNum(1, 6)` you want a 50% chance of getting a 6, and everything else has a 10% chance? –  Feb 12 '15 at 05:16

6 Answers6

3
function randomNum(low, high){
  return Math.random() > 0.5 ?
    high :
    Math.floor(Math.random() * (high - low)) + low;
}
radiaph
  • 4,001
  • 1
  • 16
  • 19
1

In a generic manner; I suppose you're after a weighted random number generator:

function weightedRandomNumber(weights) {
    var sum = 0;
    for (var w in weights) {
        w = weights[w];
        sum += w.weight;
    }

    var rand = Math.random() * sum;

    for (var w in weights) {
        w = weights[w];
        if (rand < w.weight) {
            return w.value;
        }
        rand -= w.weight;
    }

    return weights[weights.length - 1].value;
}

Test:

var config = [
    { weight: 25, value: 1 },
    { weight: 25, value: 2 },
    { weight: 50, value: 3 }
];

var test = { 1: 0, 2: 0, 3: 0 }, max = 10000;

for (var i = 1; i < max; i += 1) {
    test[weightedRandomNumber(config).toString()] += 1;
}

alert('From ' + max + ' rounds; results: ' + JSON.stringify(test));
Paul
  • 1,502
  • 11
  • 19
0

Make if else condition If it is 3 its ok or else if it is not 3 then again make a random number between 1 and 2; Hence the 3 will get 50% chances where as 1,2 will get 25% chances

Yash Thakur
  • 1,172
  • 7
  • 14
0

There are two approaches that you can use. (1) You can have array of value and random the index of value to get. If you want certain number to have higher chance, just put it more. For example:

var arr = [1, 2, 3, 3];
return arr[Math.floor(Math.random() * arr.length)];

(2) Second approach is doing the array shuffling.

var arr[1, 2, 3, 3];
shuffle(arr);
return arr[0];
Community
  • 1
  • 1
invisal
  • 11,075
  • 4
  • 33
  • 54
0

This should work:

function randomNum(low, high){
     var mid = (low + high)/2;
     var randomn = Math.floor(Math.random() * (high - low + 1)) + low;
     if(randomn > mid)
         return randomn ;
     else
         return Math.floor(Math.random() * (high - low + 1)) + low;
}
0

here you go. high will have 50% chance, the rest will split the other 50% equally

function randomNum(low, high) 
    {   
        var myarry = []
        for(var i=0;i<(high-low);i++) { myarry.push(low+i) } ; //var myarry=[low, low+1, ....,high-1] excludes high
        console.log(myarry)
        var magic=Math.random();
        var index=Math.round(magic*(high-low));    // Gaurantee the chance is split between the elements of the array
        return Math.round(magic)==1?high:myarry[index]   // Guaranteed 50% chance for high either 0 or 1, the rest will split the chance
     }