需要帮助随机 [英] need help with random

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

问题描述

嘿,..我目前正在开发一个程序,该程序应生成3个随机数并将其张贴在som标签上.

问题是有时它会生成3个数字,其中2个或全部3个相等.我该如何避免呢?

这是我的代码:


Hey there.. i am currently working on a program that should be generating 3 random numbers and post it on som labels.

the problem is that sometimes it generates 3 numbers where 2 or all 3 of them are equal. How can i avoid that?

Here is my code:


public static void newrand(Label num1, Label num2, Label num3)
       {
           Random rnd = new Random();
           int[] randomnumber = new int[3];
           for (int counter = 0; counter < 3; counter++)
           {
               randomnumber[counter] = rnd.Next(min, max);
           }
           if (randomnumber[0] != randomnumber[1])
           {
               if (randomnumber[0] != randomnumber[2])
               {
                   num1.Text = randomnumber[0].ToString();
               }
               else
               {
                   Random rnd1 = new Random();
                   int i = randomnumber[0];
                   while (i == randomnumber[0] && i == randomnumber[1] && i == randomnumber[2])
                   {
                       i = rnd1.Next(min, max);
                   }
                   num1.Text = randomnumber[0].ToString();
               }


               if (randomnumber[1] != randomnumber[0])
               {
                   if (randomnumber[1] != randomnumber[2])
                   {
                       num2.Text = randomnumber[1].ToString();
                   }
                   else
                   {
                       Random rnd1 = new Random();
                       int i = randomnumber[1];
                      while (i == randomnumber[1] && i == randomnumber[0] && i == randomnumber[2]);

                       {
                           i = rnd1.Next(min, max + 1);
                       }  num2.Text = randomnumber[1].ToString();
                   }
                   if (randomnumber[2] != randomnumber[0])
                   {
                       if (randomnumber[2] != randomnumber[1])
                       {
                           num3.Text = randomnumber[2].ToString();
                       }
                       else
                       {
                           Random rnd1 = new Random();
                           int i = randomnumber[2];
                           while (i == randomnumber[1] && i == randomnumber[0] && i == randomnumber[2])
                           {
                               i = rnd1.Next(min, max);
                           }
                           num3.Text = randomnumber[2].ToString();
                       }

                   }
               }
           }
       }

推荐答案

如果您在此处查看MSDN关于Random()的说法:http://msdn.microsoft.com/en-us/library/system.random.aspx [
解决方案是使用一个Random()对象生成多个数字.

有关更多信息,请参见MSDN页面.
If you look at what MSDN says about Random() here: http://msdn.microsoft.com/en-us/library/system.random.aspx[^] you will see that it ialks about exactly this case: "that two Random objects that are instantiated in close succession generate an identical series of random numbers."

The solution is to use one Random() object to generate multiple numbers.

See the MSDN page for more info.


使用System.Random生成的随机数取决于您在创建对象时传递的种子值.

因此,如果您要创建2个想要不同操作的随机对象,则可以传递不同的种子值,您将看到输出.

除此之外,随机调用的Default构造函数
GetTickCount API调用,该调用返回系统启动后经过的毫秒数.
http://msdn.microsoft.com/en-us/library/ms724408% 28VS.85%29.aspx [ ^ ]

因此,如果连续创建两个对象,则可能会在相同的毫秒间隔内创建它,因此将为您提供相同的输出.

我希望这会彻底清除您.

:rose:
Random numbers generated using System.Random depends on the seed value you pass while creating the object.

So if you are creating 2 random object which you want to act differently you can pass the seed value different and you will see the output.

Other than that, the Default constructor of random calls
GetTickCount API call which returns the number of milliseconds passed after the system starts up.
http://msdn.microsoft.com/en-us/library/ms724408%28VS.85%29.aspx[^]

So if you create two objects in succession, it might be created on same millisecond interval, and hence will give you the same output.

I hope this clears you totally.

:rose:


正如已经建议的那样,不要那样使用Random的多个实例.
为避免重复测试,请看一下我的提示:"从卡组中随机抽取5张卡" [ ^ ](您知道重复的卡片会令人不快).是C++,但重要的是算法.
:)
As already suggested, don''t use multiple instances of Random that way.
To avoid testing for duplicates, have a look at my tip: "Random extraction of 5 cards from a deck" [^](you know duplicated cards would be unpleasant). It is C++, but it is the algo that matters.
:)


这篇关于需要帮助随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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