1

Im using a random number function to get two random numbers between 219 and 1500. This works as expected but id like for numbers between 1200 and 1500 to be 50% less likely to occur. How can I achieve this?

I did try searching first but there is so much out there about random numbers that its difficult to find anything relevant to my issue.

 function getRandomBase (min, max) {


    orBase = Math.floor(Math.random() * (max - min + 1)) + min; 
    document.getElementById("box1").value = orBase;

    newBase = Math.floor(Math.random() * (max - min + 1)) + min; 
    document.getElementById("box3").value = newBase;


}

Demo

1
  • 1
    50% less likely than normal, or 50% less likely than numbers from the range 210 - 1200 ? Commented Feb 24, 2013 at 10:32

1 Answer 1

3

I would try something like this:

function number() {
    var isLower = (Math.random() * 3) < 2;
    if(isLower) {
        return randomNumber(219,1199);
    } else {
        return randomNumber(1200,1500);
    }
}

function randomNumber(min,max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

Updated fiddle: http://jsfiddle.net/pH3ZW/1/

Sign up to request clarification or add additional context in comments.

1 Comment

Using a random number to choose the random number method that is used... Priceless! Elegant solution, thank you very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.