vb.net Visual Basic - 使用数组和 For 循环的随机数生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29911167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Basic - Random Number Generator using Array and For Loop
提问by Afg
I need assistance with creating an elementary array in VB.NET.
我需要在 VB.NET 中创建基本数组的帮助。
The objective is to load a random number into each array location and then display the contents of the array. There are 100 index values from 1 to 100.
目标是将一个随机数加载到每个数组位置,然后显示数组的内容。从 1 到 100 有 100 个索引值。
Here is what I have so far:
这是我到目前为止所拥有的:
Dim Output As String
Dim RandomNumber As Integer
Dim n As Integer
Dim NumberArray(101) As Integer
NumberArray(n) = RandomNumber
For n = 1 To 100
Randomize()
RandomNumber = Int(Rnd() * 100) + 1
Next
Output = Output & "Index #" & n & vbTab & NumberArray(n) & vbCrLf
TextBox1.Text = Output
The output I am receiving is:
我收到的输出是:
Index #101 ?????????0
索引 #101 ?????????0
I cannot figure out how to output the entire list of arrays.
我不知道如何输出整个数组列表。
采纳答案by Paul Ishak
I suggest using RANDOM Try this:
我建议使用 RANDOM试试这个:
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Dim random As New Random(Now.Millisecond)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim array() As Integer = {}
For I As Integer = 1 To 100
ReDim Preserve array(I - 1)
array(I - 1) = random.Next(0, 101)
Next
MsgBox(arrToStr(array))
End Sub
Function arrToStr(arr As Integer()) As String
Dim sb As New System.Text.StringBuilder
For i As Integer = 0 To UBound(arr)
If i = UBound(arr) Then
sb.Append(arr(i).ToString)
Else
sb.Append(arr(i).ToString & ", ")
End If
Next
Return sb.ToString
End Function
End Class
In your style:
以你的风格:
Randomize()
Dim Output As String = String.Empty
Dim NumberArray(99) As Integer
For n As Integer = 0 To 99
Dim RandomNumber As Integer = CInt(Int(Rnd() * 100) + 1)
NumberArray(n) = RandomNumber
Output &= "Index #" & n.ToString & Space(4) & NumberArray(n) & vbCrLf
Next
TextBox1.Text = Output
回答by Paul Ishak
You could cut down on code, as follows:
您可以减少代码,如下所示:
Dim Output As String =""
Dim i As Integer
Randomize(Timer.Now)
For i = 1 To 100
Output = Output & i & vbTab & (Rnd() * 99) + 1 & vbCrLf
Next
TextBox1.Text = Output
The random numbers will be unreliable, since the default rn generator is not that good in terms of periodicity.
随机数将是不可靠的,因为默认的 rn 生成器在周期性方面并不是那么好。
回答by Justin Ryan
All your loop is doing is generating random numbers, and assigning them to a variable, which then gets overwritten in the next iteration. You need to move the array assignment and output string concatenation to inside the loop.
你的循环所做的就是生成随机数,并将它们分配给一个变量,然后在下一次迭代中被覆盖。您需要将数组分配和输出字符串连接移动到循环内部。
Dim Output As String = ""
Dim RandomNumber As Integer
Dim n As Integer
Dim NumberArray(101) As Integer
For n = 1 To 100
Randomize()
RandomNumber = Int(Rnd() * 100) + 1
NumberArray(n) = RandomNumber
Output &= "Index #" & n.ToString & vbTab & NumberArray(n).ToString & vbCrLf
Next
TextBox1.Text = Output
Please note that the code above is not 'ideal.' I specifically wrote it this way to retain as much of the asker's original code for clarity.
请注意,上面的代码不是“理想的”。为清楚起见,我专门以这种方式编写它以保留尽可能多的提问者的原始代码。

