简体   繁体   English

在标签框中生成随机数

[英]generating random numbers in labelbox

I'm trying to generate random numbers in c#.我正在尝试在 c# 中生成随机数。 When I click on the Button the lableBox1 shoud display random numbers in the given rnage and once this is done the labelBox2 should do the same.当我点击 Button 时,labelBox1 应该在给定的 rnage 中显示随机数,一旦完成,labelBox2 也应该这样做。 I numbers should start in increasing order starting from 0 to 1000. I should be able to see the change in the numbers in labelbox one at a time.the number should start from 0 and can stop anywhere within 1000 randomly.我的数字应该从 0 到 1000 以递增的顺序开始。我应该能够一次看到一个 labelbox 中数字的变化。数字应该从 0 开始,并且可以随机停止在 1000 内的任何地方。

I tried to do it.我试着去做。 But when I click on a button I'm getting random numbers in both the labelbox instantly.但是当我点击一个按钮时,我会立即在两个标签框中获得随机数。

My concern is that each lablebox should start generating the series one by one.我担心的是每个标签框都应该开始一个一个地生成系列。 numbers should start displaying from 0 in a increasing random order and halt anywhere with in 1000;数字应该从 0 开始以随机递增的顺序显示,并在 1000 的任何地方停止;

private void button1_Click(object sender, EventArgs e)
    {

        Random rnd = new Random();
        int c = rnd.Next(100);
        label1.Text = c.ToString();

        Random rng = new Random();
        int d = rng.Next(2875);
        label2.Text = d.ToString();


    }

Your code has only 7 lines but still a rather large number of issues.您的代码只有 7 行,但仍有相当多的问题。

Let's see:让我们来看看:

  • You need to be clear and precise!你需要清楚和准确! A dumb machine will have to follow your instructions.. Here seven fellow SO members hace tried to guess what you want to do and more or less failed!一台愚蠢的机器将不得不按照您的指示进行操作……这里有七位SO 成员试图猜测您想做什么,但或多或​​少都失败了!

  • You should partition your code into small functions with clear names and little or no dependencies among each other.您应该将代码划分为具有清晰名称且彼此之间很少或没有依赖关系的小函数。

  • This will (hopefully) lead to reusable code..这将(希望)导致可重用的代码..

  • You should not code any serious logic into events.您不应该将任何严肃的逻辑编码到事件中。 Instead only a few checks and calling functional methods is recommened.相反,只推荐进行一些检查和调用函数式方法。

You seem to want to generate pairs of random numbers and display them in two Labels.您似乎想要生成成对的随机数并将它们显示在两个标签中。

So I start by deciding whether the generation and the display should go into separate functions.所以我首先决定生成和显示是否应该进入单独的功能。 For any but the most trivial problems this should definitely be the case.对于除最微不足道的问题之外的任何问题,情况都应该如此。 It is called Model-View separation.它被称为Model-View分离。 But our situation is simple enough to lump them into one function:但是我们的情况很简单,可以将它们归为一个函数:

void MakeAndShowRandomPair()

We can call it on each click of your button:我们可以在您每次点击按钮时调用它:

private void button1_Click(object sender, EventArgs e)
{
    MakeAndShowRandomPair();
}

Now for the genaration of random numbers.现在用于生成随机数。 The random number genarator object has two constructors, one with a seed you can choose to control where the sequence should start;随机数生成器对象有两个构造函数,一个带有seed您可以选择控制序列应该从哪里开始; this creates a repeatable sequence.这将创建一个可重复的序列。 The other, without a parameter, uses the current time as its seed.另一个没有参数,使用当前时间作为它的种子。 One common mistake is to generate Random object so fast that they are created with the same timestamp as theirs seeds.一个常见的错误是生成 Random 对象的速度太快,以至于它们的创建时间戳与它们的种子相同 Those wil always generate the same numbers..那些总是会产生相同的数字..

So we genrate it only once at class level:所以我们在类级别只生成一次:

public Form1()
{
    InitializeComponent();
}

Random rand = new Random();

Now for the function itself.现在是函数本身。 You want the numbers to ascend.您希望数字上升。 The are two ways to achieve that:有两种方法可以实现:

  • Either store the last one and repeatedly generate one until the new one is larger than the old one.要么存储最后一个并重复生成一个,直到新的大于旧的。
  • Or add a random step或者添加一个随机步骤

I decide to use the latter approach so I need a few variables:我决定使用后一种方法,所以我需要一些变量:

int r1 = 0;
int r2 = 0;
int step1 = 10;
int step2 = 287;

void MakeAndShowRandomPair()
{
   // increase by random steps but at least 1:
   r1 += rand.Next(step1) + 1;
   r2 += rand.Next(step2) + 1;
   // display
   label1.Text = r1.ToString();
   label2.Text = r2.ToString();
}

Note that the variable names are way too short for longer programms!请注意,变量名称对于较长的程序来说太短了! For anything but such a tiny little problem longer and expressive names are obligatory!除了这样一个小问题之外,其他任何事情都需要更长和富有表现力的名字!

Now each click should fill each Label with a larger number.现在每次点击都应该用更大的数字填充每个Label

But maybe some of the commentators were right and you actually want to make this happen automatically?但也许一些评论员是对的,你真的想让这自动发生吗?

In that case the button would start (and maybe stop) a Timer and in the Timer.Tick event, guess what - You would call the very same function MakeAndShowRandomPair !在这种情况下,按钮将启动(也可能停止)一个Timer并且在Timer.Tick事件中,你猜怎么着 - 你会调用完全相同的函数MakeAndShowRandomPair So already we see the benefits of reusable code..所以我们已经看到了可重用代码的好处..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM