Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Permutations and Random

$
0
0

I have written a code block, for testing a set of numbers with a random function

firstly I get a set of 6 numbers from the randomize into a list(of Integer) but test there are no duplicates

I then add the 6 numbers to the group Numbers class to be added to a dictionary of integer and group numbers.

I sum the reject Duplicates as a key for the dictionary to test that no group numbers are the same and add the group numbers

The Problem:

If I run this I get different results in a Data grid

Sometimes I can only get 1 or 2 or 3 or 4 rows but mostly it will just sit there stuck in the loop

I think it is the random function not giving good random numbers combinations and should I not get at least 6 results?

How can I improve this code.


Class MainWindow


   Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded

      ‘ Dim listOfNumbers() As Integer = {1, 2, 3, 4, 5, 6, 7}

       Dim rejectDuplicates As New List(Of Integer)

       Dim listofGroupNumbers As New Dictionary(Of Integer, GroupNumbers)


       Dim finds As Integer = 0


       Do Until finds = 3

           Randomize()

           Dim randomNumber = CStr(Int((7) * Rnd() + 1))


           If Not rejectDuplicates.Contains(CType(randomNumber, Integer)) Then

               rejectDuplicates.Add(randomNumber)

               If rejectDuplicates.Count = 6 Then

                   Dim gn As New GroupNumbers

                   gn.g1 = rejectDuplicates(0)

                   gn.g2 = rejectDuplicates(1)

                   gn.g3 = rejectDuplicates(2)

                   gn.g4 = rejectDuplicates(3)

                   gn.g5 = rejectDuplicates(4)

                   gn.g6 = rejectDuplicates(5)



                   If Not listofGroupNumbers.Keys.Contains(rejectDuplicates.Sum) Then

                       listofGroupNumbers.Add(rejectDuplicates.Sum, gn)

                       rejectDuplicates = New List(Of Integer)

                       finds+= 1

                   End If

               End If


           End If

       Loop


       listGeneratored.ItemsSource = listofGroupNumbers.Values

   End Sub


End Class


Class GroupNumbers

   Public Property g1 As String

   Public Property g2 As String

   Public Property g3 As String

   Public Property g4 As String

   Public Property g5 As String

   Public Property g6 As String

End Class






Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>