简体   繁体   English

我如何在每次生成随机数的地方将其添加到javascript中的变量中?

[英]How would I get it where every time it generates a random number it adds it onto a variable in javascript?

How would I make it where every time it generates a random number it adds it on to a variable called Num my code is below. 我将如何在每次生成随机数时将其添加到以下代码中称为Num的变量中。

function Generate() {
    var Num;
    var RanN = Math.floor((Math.random()*10)+1);
    for (var i=0;i<4;i++) {
        Num += RanN;
    }
}

If you're trying to generate a random number i amount of times and add it to a pre-existing number, you'll want something like this: 如果您尝试生成一个随机数i次,然后将其添加到预先存在的数字中,则需要这样的代码:

function Generate(startNum, iterations) {
   for (var i=0; i<iterations; i++) {
      startNum +=  Math.floor((Math.random()*10)+1);
   }
   return startNum;
}

And you would use it like this (if you want to add ten randomly generated numbers): 您可以像这样使用它(如果您想添加十个随机生成的数字):

var num = 0;
num = Generate(num, 10);

Here's a fiddle. 这是一个小提琴。

Here is what I think the OP want: 这是我认为OP想要的:

function Generate(Num) {
    for (var i=0;i<4;i++) {
        Num += Math.floor((Math.random()*10)+1);
    }
    return Num;
}

The answers are slightly different in structure to your function. 答案在结构上与您的功能略有不同。 If you want to fix what you have (seeing that you have moved the random function into the loop), just change var Num; 如果您想解决现有问题(看到您已将随机函数移入循环),只需更改var Num; to var Num=0; var Num=0; , which will fix the NaN error (because you can't add a number to something that is not defined, because it is Not a Number) ,它将解决NaN错误(因为您不能将数字添加到未定义的内容中,因为它不是数字)

If what you want is a random int you could use: 如果您想要的是一个随机整数,则可以使用:

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

console.log(getRandomInt(1111, 9999));

You can find more info here . 您可以在此处找到更多信息。

暂无
暂无

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

相关问题 使用输入的Javascript随机数生成器(每个数字生成一次) - Javascript random number generator (generates every number once) using input 为什么我给变量加 1 但每次都加“2”? - why i add 1 to the variable but it adds "2" every time? Javascript 每次生成随机唯一数 - Javascript generate random unique number every time 制作一个函数,以每秒在Javascript中向变量添加数字 - Making a function that adds a number to a variable every second in Javascript 如何在javascript中用随机数替换每个短划线( - )? - How do I replace every dash (-) with a random number in javascript? 如何在JavaScript中以及在哪里将这两个变量结合在一起? - How and where would I combine these two variable in JavaScript? 每次程序运行时的随机数生成器(javascript) - Random number generator every time the program runs (javascript) 冷暖游戏——我怎样才能让随机数在每次用户在 JavaScript 中输入猜测数时不改变。 猜测答案没有限制 - Cold, warm game-how can I make the random number not to change every time user put a guess number in JavaScript . There is no limit on guess answer 如何制作一个每次单击时都会在显示中添加 1 的按钮? - How do I make a button that adds 1 to a display every time it is clicked? 将PHP变量传递给Javascript会在末尾加1,而不是加和 - Passing a PHP variable into Javascript adds a 1 onto the end instead of adding to the sum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM