0

I need to generate one random number between a range but exclude one specific number.
I thought I can do this like that but it didn't work.

function randNum(num){
    var randNumber=Math.floor(Math.random()*num)+1;
    if(randNumber==2){
        randNum(num);
    }else{
        alert(num);
    }
}
randNum(4);

What is the problem?
Thanks.

Skizo
  • 521
  • 2
  • 8
  • 14

5 Answers5

2

Assuming you want to output the random number and not the num-parameter, you just have to change the alert to:

alert(randNumber)

Like in this fiddle http://jsfiddle.net/3urFC/

Felix G.
  • 6,365
  • 2
  • 16
  • 24
0

Are you trying to alert the randNum that you generated? Right now you will alert the function argument (in this case the number 4) everytime. I think you want to change it to alert(randNumber).

faludi
  • 88
  • 1
  • 6
0

Math.random returns a number between 0 and 1. See the documentation on MDN.

Also, see this question about generating a random number in a specific range.

So, you have two problems: one, you're not generating a random number in the range you think you are, and two, you're not using the random number you generated.

I made this fiddle that does something similar to what you described.

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

function generateRandomWithExclusion(exclude, min, max) {
    var num = getRandomInt(min, max);

    while (num === exclude) {
        num = geRandomInt(min, max);   
    }

    return num;
}

generateRandomWithExclusion(2, 0, 10) // returns an integer between 0 and 10 that is not 2

You pass the number you want to exclude and a range of min and max, and get a random integer in that range that isn't the number you want excluded.

Community
  • 1
  • 1
Thomas Upton
  • 1,853
  • 12
  • 22
0

There is a typho as other has stated.

Also I would rearrange it this way:

function randNum(num){
    var numberToExclude=2;
    var randNumber=Math.floor(Math.random()*(num-1))+1;

    if(randNumber>=numberToExclude) {
        randNumber+=1;
    }

    alert(randNumber);
}
randNum(4);

Here is a JSFidle

http://jsfiddle.net/2F5Md/1/

Paolo
  • 15,233
  • 27
  • 70
  • 91
  • This is not a uniform distribution. – Dave Sep 28 '13 at 20:22
  • Asking for proof is a bit odd, but to explain, this will be twice as likely to pick 3 than most other numbers. Try running it a few hundred times and see for yourself. The method I suggested in the comments of the question gives an unbiased distribution. – Dave Sep 29 '13 at 13:42
  • I corrected the code. – Paolo Sep 29 '13 at 14:02
  • Still not right. You need to use `>=` and have a special case for `num<2`. And you don't need the inner check for `>num` if you do it correctly. – Dave Sep 29 '13 at 14:15
  • Here is a correct implementation which has a lot of flexibility and covers all edge cases. It also includes a testing function to prove that it is correct: http://jsfiddle.net/bpsVR/1/ – Dave Sep 29 '13 at 14:49
  • Small update for NaNs: http://jsfiddle.net/bpsVR/2/ – Dave Sep 29 '13 at 14:56
  • For comparison, here is your function in the same type of test: http://jsfiddle.net/DXXe5/ – Dave Sep 29 '13 at 15:04
  • @Dave: yes, I need to use >= Apart of this the code works. Check http://jsfiddle.net/2F5Md/ – Paolo Sep 29 '13 at 15:11
  • yes, that change does indeed fix it (http://jsfiddle.net/DXXe5/1/), although you still don't need the `>num` check. – Dave Sep 29 '13 at 15:17
  • yes Dave, no need for the >num check. You're right again. – Paolo Sep 29 '13 at 21:45
0
function randNum(num) {
    var randNumber;
    do { randNumber = Math.floor(Math.random()*num)+1; } while(randNumber==2);
    return randNumber;
}
randNum(4);
Ivan Kuckir
  • 2,327
  • 3
  • 27
  • 46