简体   繁体   English

Math.random不断返回相同的答案

[英]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. 我对编码和JavaScript还是很陌生,但是我将下面的代码作为三向随机化器编写。 Everything seems to be working fine, except that no matter how many times I run the code the return is whatever is plugged in as "c". 一切似乎都工作正常,除了无论我运行了多少次代码,返回值都是“ c”插入的。 I was wondering if anyone can give me some quick advice on what to do to fix this. 我想知道是否有人可以给我一些有关如何解决此问题的快速建议。 Thanks. 谢谢。

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

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

  document.write(compDecision)
}

A bunch of things come to mind right away: 一堆事情马上浮现在脑海:

1. JavaScript doesn't have implicit returns 1. JavaScript没有隐式返回

So this doesn't do what you think: 因此,这与您的想法不符:

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

That function returns undefined . 该函数返回undefined You need: 你需要:

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

2. Parentheses are not optional in JavaScript function calls 2.括号在JavaScript函数调用中不是可选的

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 3. JavaScript没有三向比较

So this doesn't do what you think: 因此,这与您的想法不符:

else if (33 < randomizer <= 67)

You would need: 您将需要:

else if (33 < randomizer() && randomizer() <= 67)

Lastly, as others have mentioned, defining randomizer as a function actually doesn't make sense in the first place. 最后,就像其他人提到的那样,首先将randomizer定义为函数实际上没有任何意义。 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: 为了让您的random函数执行您想要的操作(以大致相等的概率产生'a''b''c' ),您确实想在函数开始时产生一个随机值并重用它:

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

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

console.log(random());

Hopefully that helps. 希望有帮助。

You wasn't correctly calling the randomizer function. 您没有正确调用randomizer函数。 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 number
    var randomNumber = Math.random() * 100;

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

    return compDecision;
}

You can simplify your code to this: 您可以将代码简化为:

 var randomizer = Math.random() * 100; if (randomizer <= 33) { var compDecision = "a"; } else if (randomizer > 67) { var compDecision = "b"; } else if (33 < randomizer && randomizer <= 67) { var compDecision = "c"; } alert(compDecision); 

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. 但是, document.write部分可能应该分开,因为您可能希望以始终不适合将结果写入当前文档的方式调用此函数。

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: 您可以使用条件运算符:

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

document.write(random()); 

randomizer is a variable that is holding a function. randomizer是包含函数的变量。

To understand this issue, please try 要了解此问题,请尝试

alert(Math.random()); alert(Math.random());

var value = randomizer(); var value = randomizer(); alert(value); 警报(值);

I think, you can do the same thing way like this: 我认为,您可以按照以下方式做同样的事情:

function Randomize(){
    rng = Math.random() * 3;
    rng = Math.round(rng);
    switch(rng) {
    case 0:
        return "a"
        break;
    case 3:
        return "a"
        break;
    case 1:
        return "b"
        break;
    case 2:
        return "c"
        break;
    };
};
alert(Randomize());

JavaScript requires a semicolon after each statement. JavaScript在每个语句后都需要使用分号。 However, if/then/else shouldn't be followed by a semicolon. 但是,如果不应该在分号后面加上/然后/否则。

if (a < 10) {
  alert("Less than ten.");
} else {
  alert("Ten or more.");
}

Sometimes semicolons are automatically inserted where they belong, but you shouldn't rely on this -- it's best to type them yourself. 有时,分号会自动插入它们所属的位置,但是您不应该依赖它-最好自己键入它们。

The keyword var is used to create/declare/initialize a variable. 关键字var用于创建/声明/初始化变量。 This is done only once per variable. 每个变量仅执行一次。

Also it's good practice to declare a variable at the top of the function that contains it. 同样好的做法是在包含变量的函数顶部声明一个变量。

function() {
  var result;

  if (condition1 === true) {
    result = "a";
  } else if (condition2 === true) {
    result = "b"
  } else {
    result = "c"
  }

  return result;
}

In your code, "randomizer" is a function. 在您的代码中,“ randomizer”是一个函数。 To execute it and get the value it returns, you'd have to put a pair of parentheses after it: randomizer() 要执行它并获取它返回的值,您必须在其后加上一对括号: randomizer()

So randomizer <= 33 isn't comparing two numbers, it's comparing a function to a number. 因此, randomizer <= 33并不是将两个数字进行比较,而是将函数与数字进行比较。 Likewise with randomizer > 67 . randomizer > 67 Each of them therefore evaluates to false . 因此,它们每个都评估为false

Then, 33 < randomizer <= 67 evaluates to true . 然后, 33 < randomizer <= 67计算为true Why? 为什么? I'm not sure. 我不确定。 But because it's considered "true", var compDecision = "c" gets executed. 但是因为它被认为是“ true”, var compDecision = "c"将执行var compDecision = "c"

And that's why you keep getting "c". 这就是为什么您不断获得“ c”的原因。

EDIT: RobG's comment below explains why 33 < randomizer <= 67 evaluates to true in JavaScript. 编辑: RobG在下面的评论解释了为什么33 < randomizer <= 67在JavaScript中评估为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM