2011-09-17 2 views
4

이 프로그램은 파일에서 이름과 성을 생성합니다. 이 프로그램을 실행할 때 세 개의 텍스트 상자에 정보를 입력합니다. 처음 두 가지는 salary low와 high (salary1.text, salary2.text)이고 마지막 하나는 내가 원하는 "copies"(copies.text)의 수입니다. 텍스트에 10과 같은 숫자를 넣을 때 상자에 동일한 이름 중 일부가 출력됩니다.Vb.net 동일한 번호를 여러 번 생성하는 난수 생성기

firstName을 파일은 약 100 개의 레코드 을 가지고 있으며 LASTNAME 파일은 약 1000 기록

있다

왜 같은 이름 I 1000 개 사본과 같이 할 경우 문제는 더욱 심각하다

.. 그것을 생성하는 10 개 개의 레코드와 같은 일 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 

샘플 출력

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 난수 모 2 = 0'하위 비트가 적은만큼 유사 난수의 종류와 사용하는 매우 나쁜 테스트입니다 무작위로 높은 비트보다. "mod 2 = 0"은 최하위 비트를 테스트하므로 무작위 결과가 가장 적습니다. 코인 플립 (50 %) 테스트를 수행하는 더 좋은 방법은'If RandomNumber> maxLimit/2'입니다. – RBarryYoung

+0

[this NuGet package] (https://github.com/madelson/MedallionUtilities/tree/master/MedallionRandom#medallionrandom)가 도움이 될 수 있습니다. 이것으로 매번 새로운'Random'을 생성하는 대신에'Rand.Current' 인스턴스를 정적으로 사용하거나'Random'의 인스턴스를 관리하고 전달해야합니다. – ChaseMedallion

답변

19

매번 System.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-save-lang = 1 & cs-lang = vb # 코드 스 니펫 -2 – ACR