生成的随机数是唯一的(无重复)
Unique in generated random number (no duplicate)
我只想要生成的随机数不会重复,每当我尝试输入数字时,生成的随机数都有重复,任何其他想法。 我应该在这里更改什么?
看小提琴
var arr = hello.toString(10).replace(/'D/g, '0').split('').map(Number);
for(i=0;i<arr.length;i++) arr[i] = +arr[i]|0;
//initialize variables
var z = arr[3];
var y = arr[2];
var x = arr[1];
var w = arr[0];
while((((a2 <= 0) || (a2 > 49)) || ((b <= 0) || (b > 49)) || ((c <= 0) || (c > 49)) || ((d <= 0) || (d > 49)) || ((e2 <= 0) || (e2 > 49)) || ((f <= 0) || (f > 49)) || ((g <= 0) || (g > 49) ))){
//loop ulit kapag hindi match yung random number sa value nung z
while( zRandomString != z){
zRandom = Math.floor(Math.random() * (109 - 100 + 1)) + 100;
zRandomRound = zRandom % 10;
zRandomString = zRandomRound.toString();
}
var zNew = zRandom; //new value ng z
document.getElementById("zebra").innerHTML = "Z = " + zNew + "<br />";// udsadsadsad
h = zNew;
while( yRandomString != y){
yRandom = Math.floor(Math.random() * (49 - 1 + 1)) + 1;
yRandomRound = yRandom % 10;
yRandomString = yRandomRound.toString();
}
var yNew = yRandom; //new value ng z
document.getElementById("yeah").innerHTML = "Y = " + yNew + "<br />";// udsadsadsad
h = h - yNew;
while( xRandomString != x){
xRandom = Math.floor(Math.random() * (h - 1 + 1)) + 1;
xRandomRound = xRandom % 10;
xRandomString = xRandomRound.toString();
}
var xNew = xRandom; //new value ng z
document.getElementById("ex").innerHTML = "X = " + xNew + "<br />";// udsadsadsad
h = h - xNew;
while( wRandomString != w){
wRandom = Math.floor(Math.random() * (h - 1 + 1)) + 1;
wRandomRound = wRandom % 10;
wRandomString = wRandomRound.toString();
}
var wNew = wRandom; //new value ng z
document.getElementById("weh").innerHTML = "W = " + wNew + "<br />";// udsadsadsad
//h = Math.abs(h - wNew); // new value of h
h = h - wNew;
a = Math.floor(Math.random() * (wNew - 1 + 1)) + 1;
a2 = wNew - a;
b = Math.floor(Math.random() * (a2 - 1 + 1)) + 1;
c = a - b;
d = yNew;
e = Math.floor(Math.random() * (xNew - 1 + 1)) + 1;
e2 = xNew - e;
f = Math.floor(Math.random() * (e2 - 1 + 1)) + 1;
g = e - f;
}
var combo = a2.toString() + ', ' + b.toString() + ', ' + c.toString() + ', ' + d.toString() + ', ' + e2.toString() + ', ' + f.toString() + ', ' + g.toString() + ', ' + h.toString();
document.getElementById("combo").innerHTML = combo;
尝试使用此方案:
var r;
do {
r = Math.floor(Math.random() * 10);
} while (r == 6);
目前还不清楚你想要什么,但是在唯一的随机数上创建序列的一种方法是创建一个数字池并成功地从中挑选和删除项目:
function pick(pool) {
if (pool.length == 0) return undefined;
// pick an element from the pool
var k = (Math.random() * pool.length) | 0;
var res = pool[k];
// adjust array
pool[k] = pool[pool.length - 1];
pool.pop();
return res;
}
// example
var pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
while (pool.length) {
console.log(pick(pool));
}
另一种方法是先洗掉池,然后弹出元素:
function shuffle(arr) {
var n = arr.length;
while (n) {
// pick element
var k = (Math.random() * n--) | 0;
// swap last and picked elements
var swap = arr[k];
arr[k] = arr[n];
arr[n] = swap;
}
}
var pool = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
shuffle(pool);
while (pool.length) {
console.log(pool.pop());
}
如果您查看pick
和shuffle
函数,这两种方法并没有太大区别。当然,最终你会耗尽游泳池。然后,您可以决定重新创建或重新洗牌阵列。另请注意,如果池具有重复条目,则这些方法将生成重复的元素。
相关文章:
- 如何在javascript中获得与特定数字相等的随机数
- Javascript动态变量(添加了随机数)
- 使用onkeyup JS事件检查输入的值是否唯一
- 当同一浏览器的两个实例浏览时,Javascript页面如何具有唯一的ID
- Lodash映射并返回唯一
- 在javascript中添加和显示对象的随机数组中的视频
- 在数据库中循环值时,为输入框获取唯一值
- 设置随机数,然后每5秒随机减少一次,直到达到0(javascript)
- 调用的每个实例的唯一随机数
- 使用javascript生成唯一的随机数
- 生成的随机数是唯一的(无重复)
- 用唯一的随机数javascript填充数组
- 在javascript中生成10个介于1到10之间的唯一随机数
- 生成介于 0 和“x”之间的唯一随机数(整数)
- 在javascript中生成7个唯一的随机数
- 生成唯一的随机数执行插入,更新,删除显示到文本框
- 生成唯一的随机数 JavaScript
- 唯一随机数生成器问题
- javascript唯一随机数
- 唯一的随机数组javascript