调用它的每个实例的唯一随机数
Unique random number each instance it is called
我想要一个生成从 .1 到 1 的随机数的变量。在一个函数中,我想多次引用该变量,每次引用都应该生成一个唯一的数字……如果这个漫谈有意义的话。有些东西可以看一眼,但通常是用 C 或其他语言编写的。 javascript.
我需要这个
var randoms = Math.floor(Math.random() * 1) + .15;
我这里有这段代码,我在同一个函数中引用了大约 15 次。
每次我都希望数字不同。就目前而言,这会生成一个唯一编号,然后使用 15 次。
我知道它一定很简单,因为它是一个简单的概念,但我无法全神贯注。
一些指导会很棒。
也许一些上下文会有所帮助。
在这里我有一组条形变量,在时间轴上的某个点我需要它们触发宽度动画但以不同的时间速率。
下面的 var randoms 需要针对所有条形元素调用的每个实例进行更改。
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 之间的范围,Math.random() returns 从 0(含)到 1(不含)的随机值。避免 0
使用 if 语句。
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);
另一个解决方案:
您还可以将代码包装在一个函数中,并在每次调用该函数时获得一个不同的值。试试下面的代码,
HTML :
<button id="getRandom">Get Unique Number</button>
javaScript :
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');
Nikola 提供了我正在寻找的解决方案。除了我在代码中的随机拼写错误——创建一个函数来刷新随机时间变量,然后在动画时间参数所在的位置调用该特定函数的概念,效果非常好。在这种情况下,我使用了 tweenMax,但可以轻松转换为 setInterval 或任何数量的参数,这些参数每次调用时都需要一个变量进行更改。
我想要一个生成从 .1 到 1 的随机数的变量。在一个函数中,我想多次引用该变量,每次引用都应该生成一个唯一的数字……如果这个漫谈有意义的话。有些东西可以看一眼,但通常是用 C 或其他语言编写的。 javascript.
我需要这个var randoms = Math.floor(Math.random() * 1) + .15;
我这里有这段代码,我在同一个函数中引用了大约 15 次。 每次我都希望数字不同。就目前而言,这会生成一个唯一编号,然后使用 15 次。
我知道它一定很简单,因为它是一个简单的概念,但我无法全神贯注。
一些指导会很棒。
也许一些上下文会有所帮助。 在这里我有一组条形变量,在时间轴上的某个点我需要它们触发宽度动画但以不同的时间速率。
下面的 var randoms 需要针对所有条形元素调用的每个实例进行更改。
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 之间的范围,Math.random() returns 从 0(含)到 1(不含)的随机值。避免 0
使用 if 语句。
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);
另一个解决方案:
您还可以将代码包装在一个函数中,并在每次调用该函数时获得一个不同的值。试试下面的代码,
HTML :
<button id="getRandom">Get Unique Number</button>
javaScript :
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');
Nikola 提供了我正在寻找的解决方案。除了我在代码中的随机拼写错误——创建一个函数来刷新随机时间变量,然后在动画时间参数所在的位置调用该特定函数的概念,效果非常好。在这种情况下,我使用了 tweenMax,但可以轻松转换为 setInterval 或任何数量的参数,这些参数每次调用时都需要一个变量进行更改。