Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 每个实例调用的唯一随机数_Javascript_Jquery_Random - Fatal编程技术网

Javascript 每个实例调用的唯一随机数

Javascript 每个实例调用的唯一随机数,javascript,jquery,random,Javascript,Jquery,Random,我想要一个生成从.1到1的随机数的变量。在一个函数中,我想多次引用该变量,每次引用都应该生成一个唯一的数字……如果这种漫无边际的话。有一些东西扫了一眼,但通常是用C或其他语言。我需要这个javascript var randoms=Math.floor(Math.random()*1)+.15 我这里有这个代码,我在同一个函数中引用它大约15次。 每次我都希望数字不同。目前,这会生成一个唯一的数字,然后使用15次 我知道它必须是简单的,因为它是一个简单的概念,但我不能把我的思想围绕着它 一些指导

我想要一个生成从.1到1的随机数的变量。在一个函数中,我想多次引用该变量,每次引用都应该生成一个唯一的数字……如果这种漫无边际的话。有一些东西扫了一眼,但通常是用C或其他语言。我需要这个javascript

var randoms=Math.floor(Math.random()*1)+.15

我这里有这个代码,我在同一个函数中引用它大约15次。 每次我都希望数字不同。目前,这会生成一个唯一的数字,然后使用15次

我知道它必须是简单的,因为它是一个简单的概念,但我不能把我的思想围绕着它

一些指导将是惊人的

也许一些背景知识会有所帮助。 在这里,我有一组条变量,在时间轴上的某个点,我需要它们来触发宽度动画,但时间速率不同

在var下面,对于每个实例,需要更改所有条形元素的随机数

var bar1 = $('#bar1'),
    bar2 = $('#bar2'),
    bar3 = $('#bar3'),
    bar4 = $('#bar4'),
    bar5 = $('#bar5'),
    bar6 = $('#bar6'),
    bar7 = $('#bar7'),
    bar8 = $('#bar8'),
    bar9 = $('#bar9'),
    bar10 = $('#bar10'),
    bar11 = $('#bar11'),
    bar12 = $('#bar12'),
    bar13 = $('#bar13'),
    bar14 = $('#bar14'),
    bar15 = $('#bar15')
    icon = $('.icon'), 
    randoms = Math.floor(Math.random() * 1) + .15;


    init = new TimelineMax();

    init.add ("chart" , "+=2")
    init.to(icon_1, 0.5, {alpha: '1'})
        .to(icon_2, 0.5, {alpha: '1'})
        .to(icon_3, 0.5, {alpha: '1'})
        .to(icon_4, 0.5, {alpha: '1'})
        .to(bar1, randoms, {width: '100%'},'chart')
        .to(bar2, randoms, {width: '100%'},'chart')
        .to(bar3, randoms, {width: '75%'},'chart')
        .to(bar4, randoms, {width: '90%'},'chart')
        .to(bar5, randoms, {width: '85%'},'chart')
        .to(bar6, randoms, {width: '100%'},'chart')
        .to(bar7, randoms, {width: '75%'},'chart')
        .to(bar8, randoms, {width: '90%'},'chart')
        .to(bar9, randoms, {width: '90%'},'chart')
        .to(bar10, randoms, {width: '70%'},'chart')
        .to(bar11, randoms, {width: '50%'},'chart')
        .to(bar12, randoms, {width: '85%'},'chart')
        .to(bar13, randoms, {width: '80%'},'chart')
        .to(bar14, randoms, {width: '90%'},'chart')
        .to(bar15, randoms, {width: '90%'},'chart');

这是一个创建从
0.1
1.0
的随机数的函数:

var random = Math.floor(Math.random()*11) / 10 ;

如果要创建从0到n的随机数,请尝试:

var random = Math.floor( Math.random()* n+1 );

您需要决定需要多少个唯一的数字。然后,每次你得到一个数字时,将其与你以前的数字进行匹配,以获得唯一性。最后,您将拥有一组唯一的数字。对于介于.1到1之间的范围,返回0(包含)到1(排除)之间的随机值。避免使用if语句
0

var counter = 10;
var uniqueNumbers = new Array();
interval = setInterval(function(){
    var x = Number(Math.random().toFixed(1));
    if(uniqueNumbers.indexOf(x) == -1  & x != 0.0){
        uniqueNumbers.push(x);
        console.log(uniqueNumbers);
    }
    if(uniqueNumbers.length == counter){
        clearInterval(interval);
    }
}, 1);

另一种解决方案:

<button id="getRandom">Get Unique Number</button>
var counter = 0;
var numberLimit = 5;
var uniqueNumbers = new Array();

getRandom.onclick = function(){
    if(counter != numberLimit){
        CalculateRandom();
    }
};

function CalculateRandom(){
    var x = Number(Math.random().toFixed(1));
    if(uniqueNumbers.indexOf(x) == -1  & x != 0.0){
        uniqueNumbers.push(x);
        console.log(uniqueNumbers);
        counter++;
    } else if(counter != numberLimit){
        CalculateRandom();
    }
}
您还可以将代码封装在函数中,并在每次调用该函数时获得一个不同的值。请尝试下面的代码

HTML:

<button id="getRandom">Get Unique Number</button>
var counter = 0;
var numberLimit = 5;
var uniqueNumbers = new Array();

getRandom.onclick = function(){
    if(counter != numberLimit){
        CalculateRandom();
    }
};

function CalculateRandom(){
    var x = Number(Math.random().toFixed(1));
    if(uniqueNumbers.indexOf(x) == -1  & x != 0.0){
        uniqueNumbers.push(x);
        console.log(uniqueNumbers);
        counter++;
    } else if(counter != numberLimit){
        CalculateRandom();
    }
}


[注意:您也可以根据需要设置小数位。]

首先,您的代码将永远不会生成与
0.15
不同的值。您正在对随机值调用
Math.floor
,该值会立即将其转换为0。要生成介于.1到1之间的数字,请使用:

var randoms = Math.random() * 0.9 + 0.1;
其次,一旦设置了变量,它将永远不会更新并获得新值。您需要根据需要多次调用上述代码。实现这一点的一种方法是将其包装到函数中:

function randoms() {
    return Math.random() * 0.9 + 0.1;
}
console.log(randoms());
function randoms() {
    return Math.random() * 0.9 + 0.1;
}
init.to(icon_1, 0.5, {alpha: '1'})
        .to(icon_2, 0.5, {alpha: '1'})
        .to(icon_3, 0.5, {alpha: '1'})
        .to(icon_4, 0.5, {alpha: '1'})
        .to(bar1, randoms(), {width: '100%'},'chart')
        .to(bar2, randoms(), {width: '100%'},'chart')
        .to(bar3, randoms(), {width: '75%'},'chart')
        .to(bar4, randoms(), {width: '90%'},'chart')
        .to(bar5, randoms(), {width: '85%'},'chart')
        .to(bar6, randoms(), {width: '100%'},'chart')
        .to(bar7, randoms(), {width: '75%'},'chart')
        .to(bar8, randoms(), {width: '90%'},'chart')
        .to(bar9, randoms(), {width: '90%'},'chart')
        .to(bar10, randoms(), {width: '70%'},'chart')
        .to(bar11, randoms(), {width: '50%'},'chart')
        .to(bar12, randoms(), {width: '85%'},'chart')
        .to(bar13, randoms(), {width: '80%'},'chart')
        .to(bar14, randoms(), {width: '90%'},'chart')
        .to(bar15, randoms(), {width: '90%'},'chart');
编辑: 以下是如何使用上述功能:

function randoms() {
    return Math.random() * 0.9 + 0.1;
}
console.log(randoms());
function randoms() {
    return Math.random() * 0.9 + 0.1;
}
init.to(icon_1, 0.5, {alpha: '1'})
        .to(icon_2, 0.5, {alpha: '1'})
        .to(icon_3, 0.5, {alpha: '1'})
        .to(icon_4, 0.5, {alpha: '1'})
        .to(bar1, randoms(), {width: '100%'},'chart')
        .to(bar2, randoms(), {width: '100%'},'chart')
        .to(bar3, randoms(), {width: '75%'},'chart')
        .to(bar4, randoms(), {width: '90%'},'chart')
        .to(bar5, randoms(), {width: '85%'},'chart')
        .to(bar6, randoms(), {width: '100%'},'chart')
        .to(bar7, randoms(), {width: '75%'},'chart')
        .to(bar8, randoms(), {width: '90%'},'chart')
        .to(bar9, randoms(), {width: '90%'},'chart')
        .to(bar10, randoms(), {width: '70%'},'chart')
        .to(bar11, randoms(), {width: '50%'},'chart')
        .to(bar12, randoms(), {width: '85%'},'chart')
        .to(bar13, randoms(), {width: '80%'},'chart')
        .to(bar14, randoms(), {width: '90%'},'chart')
        .to(bar15, randoms(), {width: '90%'},'chart');

尼古拉提供了我一直在寻找的解决方案。撇开我在代码中的随机输入不谈——创建一个刷新随机时间变量的函数,然后在动画的时间参数所在的位置调用该特定函数的概念非常有效。在本例中,我使用了tweenMax,但可以很容易地转换为setInterval或任意数量的参数,每次调用它时都需要一个变量进行更改。

因此,将该变量放入for循环就可以了?这是不正确的。你的提琴偶尔会生成0。是的,你也可以放入定义的变量或使用静态值。是的,我刚刚注意到第一个问题,谢谢你指出这一点。第二,我试图将其转化为一个函数,以便在每次调用时都能得到一个唯一的数字。我想我真的是在用greensock vs jquery来束缚自己。我可能需要探索另一种动画方法。天哪,我从来没有想过使用函数作为时间参数,很明显,我必须试一试,看看tweening引擎是否允许这样做,尽管我看不出原因。我理解你的观点,并尝试在动画完成后更新randoms变量,以更改每个实例上生成的数量,但这不起作用,现在我必须尝试一下。谢谢,你不在乎,但我会让你了解最新情况。