Tags:
tech news
VB.NET: How To Get Random() Numbers To Array Without Duplicates Using Random.Next()
Add this code inside a button and click it, When it runs it will execute the GetRand() function which returns the result:
Dim arr As ArrayList = New ArrayList 'Declare your array Dim newNextRandom As New Random() 'Declare your random Dim rndCount As Integer 'Count how many times we loop While rndCount <= 10 'Loop until 10 Dim nexRandom As Integer = GetRand() If Not arr.Contains(nexRandom) Then 'If arr does not contain this value, we add it (prevents duplicates) arr.Add(nexRandom) 'Add random next to your If arr.Count = 10 Then 'We have our values Exit While 'Lets leave the loop End If rndCount += 1 'Count how many times we loop, use this to check with If statement if you want only a certain few random numbers End If End While For i As Integer = 0 To arr.Count - 1 'loop your array Dim num As Integer = DirectCast(arr(i), Integer) Console.WriteLine(num) 'Do stuff with the value... Next