不同的输入文本产生随机数,并确保随机数显示是唯一 [英] Generating random numbers on different input texts and ensuring random numbers shown are unique

查看:247
本文介绍了不同的输入文本产生随机数,并确保随机数显示是唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建JQuery的一个JavaScript code段具有随机数的不断变化,会在不同的文本框中显示。

不过,我无法弄清楚如何让数字显示每个文本框不同的值。我怎样才能做到这一点?

下面是我的JavaScript code迄今:

  VAR pcount = $(pcount。);
对于(VAR I = 0; I< pcount.length;我++){
变种N = [];
无功元件= pcount.eq(ⅰ);
的setInterval(函数(){
    N [i] = 10 + Math.floor(的Math.random(编号($(.pcount).VAL()))* 10);
    $('。pcount)VAL(N [I])。
},1000);}

下面是HTML code为文本框:

 <输入类型=文本级=pcount PC1值=10/>
<输入类型=文本级=pcount PC2VALUE =13/>
<输入类型=文本级=pcount PC3VALUE =16/>


解决方案

要确保随机数是真正独一无二的,你要跟踪它以前所有的随机数,如果你遇到冲突,产生一个新的

  VAR makeUniqueRandom =(函数(){
    变种偶合= {};
    返回功能(低,HI){
        VAR兰特;
        而(真){
            兰特= Math.floor((的Math.random()*(高 - 低))+低);
            如果(!偶合[兰德]){
                偶合[兰德] =真;
                返回兰特;
            }
        }
    }
})();

用法:

  VAR兰特= makeUniqueRandom(1,10);

注:你必须绝对确保你不要耗尽可用的随机数,因为如果你这样做,它会变成一个无限循环。所以,如果你问的随机数从1-10(9可能值),那么就不多叫它9倍以上。


要放就乱的值到<输入> 标签,你可以这样做:

 <输入类型=文本级=pcount PC1值=10/>
<输入类型=文本级=pcount PC2VALUE =13/>
<输入类型=文本级=pcount PC3VALUE =16/>$(。pcount)。每个(函数(){
    THIS.VALUE = makeUniqueRandom(1,10);
});

I'm trying to create a JavaScript code segment with JQuery which has random numbers that are constantly changing and will show in different text boxes.

However, I can't figure out how to make the numbers show different values for each text box. How can I accomplish this?

Here is my JavaScript code so far:

var pcount = $(".pcount");
for(var i= 0; i < pcount.length; i++){
var n = []; 
var element = pcount.eq(i);
setInterval(function() {
    n[i] = 10 + Math.floor(Math.random(Number($( ".pcount" ).val())) * 10);    
    $('.pcount').val(n[i]); 
}, 1000);}

Here is the HTML code for the text boxes:

<input type="text" class="pcount pc1" value="10"/>
<input type="text" class="pcount pc2" value="13"/>
<input type="text" class="pcount pc3" value="16"/>

解决方案

To make sure the random numbers are truly unique, you have to keep track of all random numbers that came before and if you get a conflict, generate a new one:

var makeUniqueRandom = (function() {
    var randoms = {};
    return function(low, hi) {
        var rand;
        while (true) {
            rand = Math.floor((Math.random() * (hi - low)) + low);
            if (!randoms[rand]) {
                randoms[rand] = true;
                return rand;
            }
        }
    }
})();

Usage:

var rand = makeUniqueRandom(1, 10);

Notes: You have to make absolutely sure you don't exhaust the available random numbers because if you do, it will turn into an infinite loop. So, if you're asking for random numbers from 1-10 (9 possible values), then don't call it more than 9 times.


To put random values into <input> tags, you can do this:

<input type="text" class="pcount pc1" value="10"/>
<input type="text" class="pcount pc2" value="13"/>
<input type="text" class="pcount pc3" value="16"/>

$(".pcount").each(function() {
    this.value = makeUniqueRandom(1, 10);
});

这篇关于不同的输入文本产生随机数,并确保随机数显示是唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆