【问题标题】:How could I keep Math.random() from returning a different value each time I call the variable it's stored under?每次调用存储在其下的变量时,如何防止 Math.random() 返回不同的值?
【发布时间】:2014-12-17 07:27:04
【问题描述】:

我现在有一个作业,要求我使用存储在我命名为randomNumberBetween0and5 的变量中的Math.random()(它实际上是Math.floor((Math.random() * 5) + 0);,它返回一个介于0 和5 之间的数字。我用这个数字来指代代码中其他地方的一些数组的索引。)我稍后在我的代码中再调用它两次,但我需要 Math.random() 只执行一次,然后将其值存储在 randomNumberBetween0and5 变量中以供以后使用,如果我调用而不更改变量两次或三次。

我尝试将randomNumberBetween0and5 的值存储在另一个变量中,并且(如预期的那样)这对这种情况没有帮助。任何帮助都将不胜感激!谢谢!

(这里有所有代码供您查看。)

var names = ["Lázaro", "Aviram", "Samuel", "Miles", "Felix"]; // Defining an array of random names
var people = []; // Declaring the array to hold each instance of the 'Person' object

// For() to create one new instance thrice for three names of the 'names' array in the 'Person' object defined in 'person.js'

for (var i=0;i < 3;i++) {
    var randomNumberBetween0and5 = Math.floor((Math.random() * 5) + 0); // Randomly choosing a number for the arrays to be passed to the 'Person()' object's methods defined in 'person.js'
    var newInstance = new Person.generate(names.splice(randomNumberBetween0and5,randomNumberBetween0and5+1),Person.actions[randomNumberBetween0and5],Person.jobs[randomNumberBetween0and5],i+1); // Creating a new instance of the 'Person()' object and passing a random name, action (from the Person object's properties), and job (also from the Person object's properties), along with the current loop number for the row to use (i begins at 0 so it becomes 1,2,3 instead of 0,1,2 when a 1 is added)
    //people.push(newInstance); // Adding the new instance to the 'people' array of instances
    people.push(newInstance); // Adding the newly created instance of 'Person' to the variable 'people'
}; // Ending the for()

console.log(people); // Logging the 'people' array for debugging
console.log("Main.js has been completely interpreted -- Nothing has broken yet."); // Reassuring console log to state that this file has completely executed without breaking

【问题讨论】:

  • 如果将数字存储在变量中,它将保持设置状态并且不会改变......这就是编程语言中变量的目的:保留数据。你正在用那个变量做别的事情,否则它不可能无论如何改变。
  • @MarcoBonelli variable 和 keep data 在同一个句子中不合适。也许constant 会更合适但不太正确。
  • Math.random() 在一个变量中。这意味着变量的值在每次调用时都会发生变化。我想一个常量可能不允许在Math.random() 设置它之后改变值,但我几乎可以肯定 JavaScript 没有常量。
  • 您对使用该变量时发生的情况的理解已关闭。创建变量时会调用Math.random(),但不会再调用。它只是被再次调用,因为你有它在一个循环中

标签: javascript arrays math random


【解决方案1】:

也许这个 sn-p 解释了它:

// When Math.floor is initially called it creates the random number
// store this in variable: randomNumberBetween0and5
var randomNumberBetween0and5 = Math.floor((Math.random() * 5) + 0); 

for (var i = 0; i < 10; i++) {
   // randomNumberBetween0and5 will never change in this function
   console.log(randomNumberBetween0and5);
}

您的代码中的问题是您在循环中调用 Math.floor。这将始终创建一个随机。

用你的代码试试这个:

var names = ["Lázaro", "Aviram", "Samuel", "Miles", "Felix"]; 
var people = []; 
var randomNumberBetween0and5 = Math.floor((Math.random() * 5) + 0); 

for (var i=0;i < 3;i++) {
  console.log('randomNumberBetween0and5 is: ' + randomNumberBetween0and5);
  var newInstance = new Person.generate(names.splice(randomNumberBetween0and5,randomNumberBetween0and5+1),Person.actions[randomNumberBetween0and5],Person.jobs[randomNumberBetween0and5],i+1); 
  people.push(newInstance); 
};

【讨论】:

  • 这正是我在源代码中所拥有的,但randomNumberBetween0and5 正在在我的for() 循环期间发生变化。
  • 但它在循环内。把它移到外面。
  • 我已经把它移到上面(在我的前两个变量声明下),它仍然在发生:(
  • 能否请您发布您的代码的简化版本?
【解决方案2】:

如果你把randomNumberBetween0and5做成一个数组,这样可以同时存储3个不同的数字,可以吗?

var randomNumberBetween0and5 = [];
for(var i = 0; i < 3; i++){
    randomNumberBetween0and5.push(Math.floor(Math.random()*5));
    //do stuff with randomNumberBetween0and5[i]
}

【讨论】:

    猜你喜欢
    • 2019-04-06
    • 2013-03-13
    • 2015-10-29
    • 2023-02-23
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    相关资源
    最近更新 更多