如何避免号码重复

发布于 2024-10-30 13:40:58 字数 453 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

傲世九天 2024-11-06 13:40:58

如果您想要支持 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.

浅忆 2024-11-06 13:40:58

另请注意,当像这样创建随机时:

Random r = new Random(); int newval = r.Next(10);

该类使用基于当前时间的值进行初始化。如果您将此代码放入循环中,那么您会注意到 newval 每秒仅更改几次。

最好创建一次 Random 实例,并在每次需要新的随机数时重复使用它。

Also note that when random is created like this:

Random r = new Random(); int newval = r.Next(10);

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.

芯好空 2024-11-06 13:40:58

这是随机的。根据定义,存在数字重复的统计可能性。您可以将 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.

抚你发端 2024-11-06 13:40:58

如果您不希望值重复 - 您可以在添加它们之前执行检查,看看新值是否包含在当前的值集中(如果这就是您所要求的

< strong>如果您想避免添加重复值 - 您可以这样做:

    Random r = new Random();
    int newval = r.Next(10);

    //Checks for containment
    if(!values.Contains(newval))
    {
        values.Add("key",newval);
    }

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:

    Random r = new Random();
    int newval = r.Next(10);

    //Checks for containment
    if(!values.Contains(newval))
    {
        values.Add("key",newval);
    }
无妨# 2024-11-06 13:40:58

在将值添加到哈希表并重新生成之前,您应该检查哈希表。那么你最终会得到一个递归函数!

You should check your hashtable before adding values into it and re-generate. You would end up with a recursive function then!

_蜘蛛 2024-11-06 13:40:58

您使用 Hashtable 而不是 是否有特殊原因哈希集?使用 HashSet,您可以轻松生成 N 个唯一的随机数:

HashSet<int> Keys = new HashSet<int>();
Random rnd = new Random();
while (Keys.Count < 10)
{
    Keys.Add(rnd.Next(10));
}

如果满足以下条件,Add 方法将返回 false要添加的值已在集合中,并且不会再次添加该值。

Is there a particular reason you're using Hashtable rather than HashSet? With HashSet<int>, you can easily generate N unique random numbers:

HashSet<int> Keys = new HashSet<int>();
Random rnd = new Random();
while (Keys.Count < 10)
{
    Keys.Add(rnd.Next(10));
}

The Add method will return false if the value to be added is already in the collection, and that value won't be added again.

秋千易 2024-11-06 13:40:58

至少,在处理程序外部创建 Random 实例。

Random r = new Random();
Hashtable values = new Hashtable();

protected void Button1_Click(object sender, EventArgs e)
{
    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);
    }
}

At the very least, create Random instance outside the handler.

Random r = new Random();
Hashtable values = new Hashtable();

protected void Button1_Click(object sender, EventArgs e)
{
    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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文