JavaScript的每一次随机生成的唯一编号 [英] Javascript generate random unique number every time

查看:442
本文介绍了JavaScript的每一次随机生成的唯一编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

行,所以我需要创建1-10之间的四个随机生成的数字,他们可以是不一样的。所以我的想法是,以每个号码添加到一个数组,但我怎么能检查,看看是否数量是数组中,如果是,重新生成的数量,如果它不是新的电话号码添加到阵列?

Ok so i need to create four randomly generated numbers between 1-10 and they cannot be the same. so my thought is to add each number to an array but how can I check to see if the number is in the array, and if it is, re-generate the number and if it isnt add the new number to the array?

所以基本上它会去,

1,创建新的号码,并添加到阵列
2.创建第二个新号,查,看它是否已经存在,如果不存在,添加到阵列。如果它确实存在,重新创建新号,等再次检查...
3.same如上等

1.create new number and add to array 2.create second new number, check to see if it exist already, if it doesn't exist, add to array. If it does exist, re-create new number, check again etc... 3.same as above and so on.

推荐答案

您想要的东西被称为随机摸彩袋。考虑你有一个数字的包,每个号码只有在重新这款包包psented一次$ P $。你拿号码的开出,随意,为尽可能多的,因为你需要。

You want what is called a 'random grab bag'. Consider you have a 'bag' of numbers, each number is only represented once in this bag. You take the numbers out, at random, for as many as you need.

一些这里psented其他解决方案$ P $的问题是,它们随机生成的号码,并检查,看它是否已经被使用。这将需要更长的时间和更长的时间才能完成(理论上可达时间是无限的),因为你在等待随机的()函数返回你不已经有一个值(和它没有的带有要做到这一点,它可以给你永远的1-9,但永远不会返回10)。

The problem with some of the other solutions presented here is that they randomly generate the number, and check to see if it was already used. This will take longer and longer to complete (theoretically up to an infinite amount of time) because you are waiting for the random() function to return a value you don't already have (and it doesn't have to do that, it could give you 1-9 forever, but never return 10).

有很多方法来实现抓斗袋型解决方案,每个有不同程度的成本(虽然,如果做得正确,永远不会是无限的)的

There are a lot of ways to implement a grab-bag type solution, each with varying degrees of cost (though, if done correctly, won't ever be infinite).

最基本的问题的解决方案将是如下:

The most basic solution to your problem would be the following:

var grabBag = [1,2,3,4,5,6,7,8,9,10];

// randomize order of elements with a sort function that randomly returns -1/0/1
grabBag.sort(function(xx,yy){ return Math.floor(Math.random() * 3) - 1; })

function getNextRandom(){
    return grabBag.shift();
};

var originalLength = grabBag.length;
for(var i = 0; i < originalLength .length; i++){
    console.log(getNextRandom());
}

这是当然的破坏原始grabBag阵列。而且我不知道如何真正随机的排序是,但是对于很多应用来说可能是足够好。

This is of course destructive to the original grabBag array. And I'm not sure how 'truly random' that sort is, but for many applications it could be 'good enough'.

这是稍微不同的方法将是所有未使用的元素存储在数组中,随机选择一个索引,并且然后该索引位置取出元件。这里的成本是如何频繁创建/每次你删除一个元素时毁坏阵列。

An slightly different approach would be to store all the unused elements in an array, randomly select an index, and then remove the element at that index. The cost here is how frequently you are creating/destroying arrays each time you remove an element.

这篇关于JavaScript的每一次随机生成的唯一编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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