2011-09-17 116 views
4

我有這個程序生成從文件名字和姓氏。當我運行這個程序時,我在三個文本框中輸入信息。前兩個是工資低和高(salary1.text,salary2.text),最後一個是我想「複製」(copies.text)的數量。當我在文中提出一個數字,如10它會輸出一些相同的名字。Vb.net隨機數發生器產生相同數量的多次

的名字文件大約有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 
+1

除了其中有人已經指出的種子/定時問題,測試'IF RandomNumber模2 = 0'是非常糟糕的測試作爲下位少與這種類型的僞隨機數字發生器的使用隨機比較高位。 「mod 2 = 0」測試最低位,因此具有最少的隨機結果。進行投幣式(50%)測試的更好方法是如果RandomNumber> maxLimit/2。 – RBarryYoung

+0

您可能會發現[這個NuGet包(https://github.com/madelson/MedallionUtilities/tree/master/MedallionRandom#medallionrandom)有幫助。有了這個,你可以使用靜態的Rand.Current實例,而不是每次創建一個新的Random,或者必須管理和傳遞一個Random實例。 – ChaseMedallion

回答

19

您使用的每一次System.Random一個新的實例。 Random按當前時間播種。

初始化Random類的新實例,使用時間相關的默認種子值

Reference

既然你是在連續快速地創建新實例,他們得到相同的種子。

相反,你應該使它成爲一個領域,並初始化爲字段初始或構造函數中使用的Random相同的情況下,有可能。例如:

Public Class Form1 
    Private _random As New System.Random() 

    'Use _random in other methods. 
End Class 
+0

在Random類MSDN文章有一個很好的例子,顯示返回相同的數字序列的隨機的兩個實例:https://msdn.microsoft.com/en-us/library/system.random(v=vs.110 ).ASPX?CS-保存琅= 1&CS琅= VB#代碼片斷-2- – ACR