Skip to content Skip to sidebar Skip to footer

Math.random Keeps Returning The Same Answer

I'm still pretty new to coding and javascript, but I wrote the code below as a three way randomizer. Everything seems to be working fine, except that no matter how many times I run

Solution 1:

A bunch of things come to mind right away:

1. JavaScript doesn't have implicit returns

So this doesn't do what you think:

var randomizer = function() {
  Math.random() * 100
}

That function returns undefined. You need:

var randomizer = function() {
  returnMath.random() * 100
}

2. Parentheses are not optional in JavaScript function calls

So this also doesn't do what you think:

if (randomizer <= 33) {
    var compDecision = "a"
}

You would need:

if (randomizer() <= 33) {
    var compDecision = "a"
}

3. JavaScript doesn't have three-way comparisons

So this doesn't do what you think:

elseif (33 < randomizer <= 67)

You would need:

elseif (33 < randomizer() && randomizer() <= 67)

Lastly, as others have mentioned, defining randomizer as a function actually doesn't make sense in the first place. For your random function to do what you want (produce 'a', 'b', or 'c' with roughly equal probability), you really want to produce a single random value at the start of the function and reuse it:

functionrandom() {
  var randomizer = Math.random() * 100;

  if (randomizer <= 33) {
    return'a';
  } elseif (randomizer <= 67) {
    return'b';
  } else {
    return'c';
  }
}

console.log(random());

Hopefully that helps.

Solution 2:

Dan Tao's answer is great, in addition there doesn't seem to be any point to a one–line function that is only called once, so:

var n = Math.random() * 100;

However, the document.write part probably should be separate as you likely want to call this function in ways where always writing the result to the current document isn't appropriate.

Lastly, you only need test two of the three conditions since if it isn't either of the first two, it must be the third. You use the conditional operator for that:

functionrandom() {
  var n = Math.random() * 100;
  return n <= 33? 'a' : n <= 67? 'c' : 'b';
}

document.write(random()); 

Solution 3:

You wasn't correctly calling the randomizer function. I've removed it as it wasn't really necessary in this case:

var random = function() {
    // I assume you want the comparisons below to be run against the same numbervar randomNumber = Math.random() * 100;

    if (randomNumber <= 33) {
        var compDecision = "a";
    }
    elseif (randomNumber > 67) {
        var compDecision = "b"
    }
    else {
        var compDecision = "c"
    }

    return compDecision;
}

Solution 4:

You can simplify your code to this:

var randomizer = Math.random() * 100;

if (randomizer <= 33) {
  var compDecision = "a";
} elseif (randomizer > 67) {
  var compDecision = "b";
} elseif (33 < randomizer && randomizer <= 67) {
  var compDecision = "c";
}

alert(compDecision);

Solution 5:

randomizer is a variable that is holding a function.

To understand this issue, please try

alert(Math.random());

var value = randomizer(); alert(value);

Post a Comment for "Math.random Keeps Returning The Same Answer"