如何连续避免2个相同的值(new random()) [英] How to avoid 2 same value in a row (new random())

查看:214
本文介绍了如何连续避免2个相同的值(new random())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

只是一个简单的问题:



我有一个按钮'随机'。如果我按下按钮,程序会将标签文本设置为此值。

有时,如果我按下按钮2次,则返回相同的数字。



我怎样才能避免获得两个相同的值?



我尝试了什么:



Hey,
just a quick question:

I hava a button 'get Random'. If i press the button, the program sets the text of a label to this value.
sometimes, if i press the button 2 times, it returns the same number.

How can i avoid getting two same values?

What I have tried:

int newRandom = 0;
int lastRandom = 1;

private int GetRandom()
{
   Random r = new Random();
   newRandom = r.Next(0, 7);

   while (newRandom == lastRandom)
   {
      newRandom = r.Next(0, 7);
      if(newRandom != lastRandom) break;
   }
   lastRandom = newRandom;

   return newRandom;
}


label.Text = GetRandom.ToString();

推荐答案

随机数使用所谓的种子作为模拟随机性的数学方程的起始值。如果给随机数生成器提供相同的种子,您将始终获得相同的值序列,因此第一个数字将始终相同。你可以在构造函数中指定种子



随机(yourSeedHere)



为了真正随机种子也需要随机...所以你需要一个随机数来得到随机数,这是怎么回事?



当你离开种子的时候在你的代码中(你使用随机参数构造函数)



Random numbers use what's known as a "seed" as the starting value to a mathematical equation that simulates randomness. If you give the random number generator the same seed you'll always get the same sequence of values, so the first number will always be the same. You can specify the seed in the constructor

Random(yourSeedHere)

In order to be truly random the seed also needs to be random...so you need a random number to get random numbers, how does that work?

When you leave off the seed as you have in your code (you use the parameterless constructor for Random)

Random r = new Random();





然后它使用当前时间作为种子,或者更准确地说当前时间为蜱。因此,当您创建两个Random实例时,它们会在不同时间创建,因此请使用不同的种子。但是,如果您在每个tick发送多次的循环中运行代码,则所有实例都使用相同的种子,因此生成相同的数字。



所有的解决方案这很简单...创建一次Random类并重复使用它而不是每次都创建一个新类。





then it uses the current time as the seed, or more accurately the current number of "ticks". So when you create two instances of Random, they are created at different times so use different seeds. However, if you run your code in a loop that fires multiple times per tick, all instances uses the same seed so generate the same number.

The solution to all of this is simple...create the Random class once and re-use it rather than creating a new one every time.

Random r = new Random();
int newRandom = 0;
int lastRandom = 1;
 
private int GetRandom()
{
   newRandom = r.Next(0, 7);
 
   while (newRandom == lastRandom)
   {
      newRandom = r.Next(0, 7);
      if(newRandom != lastRandom) break;
   }
   lastRandom = newRandom;
 
   return newRandom;
}


这是随机数的本质:这个想法是下一个数字不依赖于先前的数值,就像你的时候一样掷硬币:头部的几率为50:50,无论你最后一次抛出它的价值如何。并且样本量越小,您获得重复的可能性就越大。因此,如果选择七个值,则连续重复的概率为1:7。



所以...设置一个数组和一个变量:

That is the nature of random numbers: the idea is that the next number is not dependant on the previous value, much like when you toss a coin: the chances of a head are 50:50 regardless of the value when you last tossed it. And the smaller the sample size the more likely it is that you will get duplicates. So with a choice of seven values, you have a 1:7 chance of consecutive duplicates.

So...set up an array and a variable:
private List<int>values = new List<int> () { 1, 2, 3, 4, 5, 6, 7 };
private int last = -1;
private Random random = new Random();



然后使用:


Then use that:

public int GetRandom()
    {
    if (last < 0)
        {
        int index = random.Next(0, 7);
        last = values[index];
        values.RemoveAt(index);
        }
    int result = values[random.Next(0, 6)];
    values.Remove(result);
    values.Add(last);
    last = result;
    return result;
    }



这样就不会重复了!


That'll give no duplicates!


这篇关于如何连续避免2个相同的值(new random())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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