gpt4 book ai didi

Vb.net 随机数生成器多次生成相同的数字

转载 作者:行者123 更新时间:2023-12-04 16:26:21 27 4
gpt4 key购买 nike

我有这个程序从文件中生成名字和姓氏。当我运行这个程序时,我在三个文本框中输入信息。前两个是salary low和high(salary1.text,salary2.text),最后一个是我想要的“份数”(copies.text)..当我在文本中输入10这样的数字时框它输出一些相同的名称。

名字文件有大约 100 条记录
并且姓氏文件有大约 1000 条记录

为什么它生成相同的名称

如果我做 1000 份之类的事情,问题会更糟。它输出相同的东西 8 次,然后再做 8 次不同的东西

Public Class Form1

Dim sex As String

Function randomfirstname()
Dim infile As IO.StreamReader
Dim infile1 As IO.StreamReader
Dim male() As String
Dim female() As String
Dim name As String
Dim n As Integer = 0
Dim fun As New System.Random
Dim maleorfemale As New Random()
Dim RandomNumber As Integer
Dim index As Integer
RandomNumber = maleorfemale.Next(0, 55984)
infile = IO.File.OpenText("boysnames.txt")
infile1 = IO.File.OpenText("girlsnames.txt")

If RandomNumber Mod 2 = 0 Then
While infile.Peek <> -1
ReDim Preserve male(n)
male(n) = infile.ReadLine
n = n + 1
End While
n = n - 1
index = fun.Next(0, n)
name = male(index)
sex = "M"
n = 0
Return name


Else
While infile1.Peek <> -1
ReDim Preserve female(n)
female(n) = infile1.ReadLine
n = n + 1
End While
n = n - 1
index = fun.Next(0, n)
name = female(index)
sex = "F"
Return name
n = 0
End If
End Function
Function randomlastname()
Dim infile2 As IO.StreamReader
Dim lname() As String
Dim lastname As String
Dim n As Integer = 0
Dim index As Integer
Dim fun As New System.Random
infile2 = IO.File.OpenText("lastname.txt")
While infile2.Peek <> -1
ReDim Preserve lname(n)
lname(n) = infile2.ReadLine
n = n + 1
End While
n = n - 1
index = fun.Next(0, n)
lastname = lname(index)
Return lastname
End Function
Function salary()
Dim salary01 As Double
Dim salary02 As Double
Dim salary03 As Double
salary01 = CDbl(salary1.Text)
salary02 = CDbl(salary2.Text)
Dim sal As New System.Random


salary03 = System.Math.Round(sal.NextDouble() * (salary02 - salary01) + salary01, 2)
Return salary03
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'ListBox1.Items.Add(randomfirstname() & vbTab & randomlastname() & vbTab & sex & vbTab & salary())
Dim outfile As New System.IO.StreamWriter("C:\Users\Johnathon\Desktop\486assign1.txt")
Dim i As Integer = 0
outfile.Write("Firstname" & vbTab & "LastName" & vbTab & "Sex" & vbTab & "Salary" & vbCrLf)
outfile.Write("-----------------------------------------------------------------------------" & vbCrLf)

For i = 1 To CInt(copies.Text)
outfile.Write(randomfirstname() & vbTab & randomlastname() & vbTab & sex & vbTab & salary() & vbCrLf)
ListBox1.Items.Add(randomfirstname() & vbTab & randomlastname() & vbTab & sex & vbTab & salary())
Next
outfile.Close()

End Sub
End Class

具有 10 条记录的示例输出
Firstname   LastName    Sex Salary
-----------------------------------------------------------------------------
Carson Gillespie M 8.46
Carson Gillespie M 8.46
Carson Gillespie M 8.46
Samantha Daniels F 5.84
Samantha Daniels F 5.84
Samantha Daniels F 5.84
Natalia Guthrie F 9.26
Natalia Guthrie F 9.26
Natalia Guthrie F 9.26
Natalia Guthrie F 6.64

最佳答案

您正在使用 System.Random 的新实例每次。 Random由当前时间播种。

Initializes a new instance of the Random class, using a time-dependent default seed value

Reference


由于您正在非常快速地连续创建新实例,因此它们会获得相同的种子。
相反,您应该使用 Random 的相同实例。 ,可能通过将其设为字段并初始化为字段初始值设定项或构造函数。例如:
Public Class Form1
Private _random As New System.Random()

'Use _random in other methods.
End Class

关于Vb.net 随机数生成器多次生成相同的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7456477/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com