Vb.net随机数生成器多次生成相同的数字 [英] Vb.net Random Number generator generating same number many times

查看:259
本文介绍了Vb.net随机数生成器多次生成相同的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让该程序从文件生成名字和姓氏.当我运行该程序时,我在三个文本框中输入信息.前两个是薪金的低和高(salary1.text,salary2.text),最后一个是我想要的份数"(copies.text)的数量.当我在文本中输入数字10时框,它输出一些相同的名称.

I have this program generating a firstname and lastname from a file. When I run this program I enter information in three textboxes. The first two are the salary low and high(salary1.text, salary2.text) and the last one is the number of "copies"(copies.text) that I want.. When I put a number such as 10 in the text box it outputs some of the same names.

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

The firstname file has around 100 records and the lastname file has about 1000 records

为什么会产生相同的名字

Why is it generating the same name

如果我做1000份这样的事情,问题就更糟了.它输出8次相同的东西,然后又输出8次不同的东西

The problem is even worse if I do something like 1000 copies.. it outputs the same thing 8 times and then does something different another 8 times

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条记录的样本输出

Sample Output with 10 records

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是当前时间的种子.

You're using a new instance of System.Random every time. Random is seeded by the current time.

使用时间相关的默认种子值初始化Random类的新实例

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

参考

由于您要快速连续创建新实例,因此它们会获得相同的种子.

Since you are creating new instances in very quick succession, they get the same seed.

相反,您应该使用Random的相同实例,可能将其设为一个字段并初始化为字段初始化程序或构造函数.例如:

Instead, you should use the same instance of Random, possibly by making it a field and initializing as a field initializer or constructor. For example:

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

    'Use _random in other methods.
End Class

这篇关于Vb.net随机数生成器多次生成相同的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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