jeudi 27 avril 2017

VB.net Random number generator is repeating the same value

I am trying to create an array of random values so that I can bubble sort them. The problem is, the random number generator is not working and repeats the same number over and over. I've used this method of random number generating before and its worked just fine. If I put a breakpoint into the code in the CreateArray() loop, then it works just fine, but I have to step through the code manually. I can't understand how it works with a breakpoint, but doesn't work without one.

Sub Main()
    Dim Value_Array As New List(Of Integer)
    Dim No_Of_Items As Integer = 10
    Dim Pass_No As Integer = 1
    Dim Temp As Integer
    Value_Array = CreateArray(No_Of_Items)
    For Pass_No = 1 To (No_Of_Items - 1)
        For j = 0 To (No_Of_Items - (Pass_No + 1))
            If Value_Array(j) > Value_Array(j + 1) Then
                Temp = Value_Array(j)
                Value_Array(j) = Value_Array(j + 1)
                Value_Array(j + 1) = Temp
            End If
        Next
    Next
    For i = 0 To (No_Of_Items - 1)
        Console.Write(Value_Array(i) & ", ")
    Next
    Console.ReadKey()
End Sub
Function GenerateRandomNumber()
    Dim Random_Number As Random = New Random()
    Dim Random As Integer = Random_Number.Next(0, 101)
    Return Random
End Function
Function CreateArray(No_Of_Items)
    Dim Count As Integer = 0
    Dim Temp_Array As New List(Of Integer)
    Do Until Count = No_Of_Items
        Temp_Array.Add(GenerateRandomNumber())
        Count = Count + 1
    Loop
    Return Temp_Array
End Function




Aucun commentaire:

Enregistrer un commentaire