0

How can I generate a random number between 1 - 10 except that the random number can't be 3

J. Steen
  • 15,470
  • 15
  • 56
  • 63
12345
  • 96
  • 1
  • 7

7 Answers7

5

Get a random number between 1 and 9 and then add one if it's 3 or greater, or

better, just change any 3s into 10s.

function getNumber() {
    return (n = 9 * Math.ceil(Math.random())) === 3? 10: n;
}
Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • 2
    @mac10688 How so? You can easily [generate a random **integer**](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/random#Examples) from `[1...9]` (inclusive) without any skewing (by always rounding down). Adding 1 when the result is greater than or equal to 3 will simply transform the 'source' to `[1...2] U [4...10]`. It's perfectly valid and more efficient than a solution with a loop rejecting 'faulty' results. – Mattias Buelens Jan 27 '13 at 15:35
2

Based on this nice answer:

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

var rand;
while((rand = getRandomInt(1, 10)) == 3);
// rand is now your random number
Community
  • 1
  • 1
dan-lee
  • 14,365
  • 5
  • 52
  • 77
0

This should work.

var r = 3;
while(r == 3) r = Math.ceil(Math.random() * 10);
techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • Do not just apply the answer, do read up about Math functions like ceil and random so you know why it works. – techfoobar Jan 27 '13 at 15:01
0
function rand(begin, end) {
    var result = Math.floor( Math.random() * (end - begin + 1) ) + begin;

    return result === 3 ? rand(begin, end) : result;
}
David G
  • 94,763
  • 41
  • 167
  • 253
0
function rand(){
var r = Math.ceil(Math.random() * 10);
if (r==3){
return rand()}
else
return r;
}
Arpit
  • 12,767
  • 3
  • 27
  • 40
0

Here's a short, quick solution, using a self-executing function, that does what you need exactly but is only useful for the specific circumstance you describe:

var randOneToTenButNotThree = function () {
    var result = Math.floor(Math.random() * 10) + 1; // PICK A NUMBER BETWEEN 1 AND 10
    return (result !== 3) ? result : randOneToTenButNotThree(); // IF THE NUMBER IS NOT 3 RETURN THE RESULT, OTHERWISE CALL THIS FUNCTION AGAIN TO PICK ANOTHER NUMBER
}

var result = randOneToTenButNotThree(); // RESULT SHOULD BE A NUMBER BETWEEN 1 AND 10 BUT NOT 3

However, you could abstract this out to produce a random number in any given range, excluding any number of your choice:

var randExcl = function (lowest, highest, excluded) {
    var result = Math.floor(Math.random() * (highest - lowest)) + lowest;
    return (result !== excluded) ? result : randExcl();
}

var result = randExcl();

Just don't forget that if the function is renamed, you should also change the reference to it from within at the end of that return statement so that it can keep calling itself whenever it produces the excluded number.

guypursey
  • 3,114
  • 3
  • 24
  • 42
0

function r(){a = Math.floor(Math.random() * 10) + 1; if (a==3) a++; return a;}

Meetai.com
  • 6,622
  • 3
  • 31
  • 38