如何避免号码重复
Hashtable values = new Hashtable();
protected void Button1_Click(object sender, EventArgs e)
{
Random r = new Random();
int newval = r.Next(10);
values.Add("key",newval);
//foreach (object value in values.Values)
//{
// Response.Write(value.ToString());
//}
ArrayList arrayList = new ArrayList(values.Values);
foreach (int key in arrayList)
{
Response.Write(key);
}
}
Hashtable values = new Hashtable();
protected void Button1_Click(object sender, EventArgs e)
{
Random r = new Random();
int newval = r.Next(10);
values.Add("key",newval);
//foreach (object value in values.Values)
//{
// Response.Write(value.ToString());
//}
ArrayList arrayList = new ArrayList(values.Values);
foreach (int key in arrayList)
{
Response.Write(key);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您想要支持 n 个值,并且只希望它们使用一次,常见的技术是提前对这些值进行洗牌,然后简单地按顺序返回它们。否则,与消耗的每个连续值发生冲突的概率将会越来越高。
研究诸如 Fisher-Yates 之类的洗牌算法。
If you have n values you want to support and you only want them to be used once, a common technique is to shuffle those values in advance and then simply return them sequentially. Otherwise, you'll have higher and higher probability of collision with each successive value that is consumed.
Look into shuffling algorithms such as Fisher-Yates.
另请注意,当像这样创建随机时:
该类使用基于当前时间的值进行初始化。如果您将此代码放入循环中,那么您会注意到 newval 每秒仅更改几次。
最好创建一次 Random 实例,并在每次需要新的随机数时重复使用它。
Also note that when random is created like this:
the class is initialized with current-time-based value. If you put this code in a loop then you will notice that newval is changed only few times per second.
It's better to create Random instance once and reuse it each time you need new random number.
这是随机的。根据定义,存在数字重复的统计可能性。您可以将 1-10 之间的 10 个随机整数添加到一个列表中,并且它们不会重复,但这不太可能。
理想情况下,每次调用随机化函数时,获得任何一个数字的几率与获得任何其他数字(包括上次获得的数字)的几率完全相同。
It's random. By definition there exists a statistical possibility that numbers will repeat. It's possible that you can add 10 random integers from 1-10 to a list and that they wouldn't repeat, but it would be unlikely.
Ideally, each time you call a randomization function, you have exactly the same odds of getting any one number as you do of getting any other number, including the number you got last time.
如果您不希望值重复 - 您可以在添加它们之前执行检查,看看新值是否包含在当前的值集中(如果这就是您所要求的)
< strong>如果您想避免添加重复值 - 您可以这样做:
If you don't want values to repeat - you could perform a check prior to adding them to see if the new value is contained in the current set of values (if this is what you are asking)
If you want to avoid adding in duplicate values - into values you could do this:
在将值添加到哈希表并重新生成之前,您应该检查哈希表。那么你最终会得到一个递归函数!
You should check your hashtable before adding values into it and re-generate. You would end up with a recursive function then!
您使用
Hashtable
而不是 是否有特殊原因哈希集?使用HashSet
,您可以轻松生成N
个唯一的随机数:如果满足以下条件,
Add
方法将返回false
要添加的值已在集合中,并且不会再次添加该值。Is there a particular reason you're using
Hashtable
rather than HashSet? WithHashSet<int>
, you can easily generateN
unique random numbers:The
Add
method will returnfalse
if the value to be added is already in the collection, and that value won't be added again.至少,在处理程序外部创建
Random
实例。At the very least, create
Random
instance outside the handler.