文本框中的多个随机和增加值 [英] Multiple random and increase value in a Textbox

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

问题描述

我想用这段代码创建,做多个随机(即循环)做一个随机20-30次.我怎么能那样做?我想在每次进入下一个随机步骤时增加 +1 文本框的值.

I would like to create with this code, to do a multiple random (ie a loop) to do a random 20-30 times. how could i do that? and I want to increase the value of the +1 textbox each time it goes to the next random step.

TextBox1.Text = Val(TextBox1.Text) + 1

代码:

   Dim strWords As String() = str1.Split(",")
        'Create an instance of the Random class
        Dim ValRnd As Integer = TxtNumRnd.Text
        Dim rnd As New Random(ValRnd)' (Values Random not worked)
        'Get a random number from 1 to 80  (2 digits)
        TextBox1.Text = Val(TextBox1.Text) + 1
        Dim randomNumber As Integer = rnd.Next(0, 81)
        If randomNumber = strWords(StrwrVal.Text) Then
            Exit For
        Else
            TxtRnd1.Text = TxtRnd1.Text & vbNewLine & randomNumber
        End If
    Next
Next

随机值不起作用.

Dim rnd As New Random(5)'

Dim rnd As New Random(5)'

推荐答案

虽然这可能不是一个直接的答案,但它应该有助于澄清一些事情.

While this may not be a direct answer it should serve to clarify a few things.

随机构造函数 - 正如我在评论中所述,使用 New Random(5) 不会为您生成 5 个随机数的序列.它只是为您的随机数生成器设置种子.这意味着您在调用 Random.Next() 时生成的数字序列将遵循相同的模式,因为它们都具有相同的种子(请参阅 示例).

The Random Constructor - as I stated in the comments, using New Random(5) does not generate a sequence of 5 random numbers for you. It simply sets the seed for your random number generator. This means that the sequence of numbers that you generate when calling Random.Next() will follow the same pattern as they all have the same seed (see example).

注意:理想情况下,在创建 New Random()不要设置种子值.它们的当前时间将用作默认种子.

Note: Ideally, when creating a New Random() do not set a seed value. They current time will be used as the default seed.

<小时>

在具有定义种子的循环内创建新的 Random(x)


Creating a New Random(x) inside a loop with a defined seed

'Since random is declared inside the loop, using the same seed value 
'each time the loop executes, the same random sequence would be generated.
'Random.Next() will then continually access the first value in the sequence. 
For i = 1 To 5
    Dim rnd As New Random(5)
    TextBox1.AppendText($"{rnd.Next(0, 11)} | ")
Next

输出:3 |3 |3 |3 |3

使用定义的种子在循环外创建一个新的 Random(x)

Creating a New Random(x) outside of the loop with a defined seed

'Since random is now declared outside the loop, and Random.Next() is called
'inside the loop, the output sequence actually progresses.
'Note that the first number is the same as the previous example as the seed is the same. 
Dim rnd As New Random(5)
    For i = 0 To 5
    TextBox1.AppendText($"{rnd.Next(0, 11)} | ")
Next

输出:3 |3 |2 |6 |5 |10

创建多个新随机(x)实例全部使用相同的种子

        'Instance 1
        Dim rnd1 As New Random(5)
        For i = 0 To 5
            TextBox1.AppendText($"{rnd1.Next(0, 11)} | ")
        Next

        'Instance 2
        Dim rnd2 As New Random(5)
        For i = 0 To 5
            TextBox2.AppendText($"{rnd2.Next(0, 11)} | ")
        Next

        Instance 3
        Dim rnd3 As New Random(5)
        For i = 0 To 5
            TextBox3.AppendText($"{rnd3.Next(0, 11)} | ")
        Next

实例 1 输出:3 |3 |2 |6 |5 |10
实例 2 输出:3 |3 |2 |6 |5 |10
实例 3 输出:3 |3 |2 |6 |5 |10

这篇关于文本框中的多个随机和增加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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