Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,953 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,332 people online right now. Registration is fast and FREE... Join Now!




Trying to Produce the numbers (1-8) randomly and mutually exclusive

 
Reply to this topicStart new topic

Trying to Produce the numbers (1-8) randomly and mutually exclusive

Schmit38
21 Dec, 2007 - 11:28 AM
Post #1

New D.I.C Head
*

Joined: 21 Dec, 2007
Posts: 26



Thanked: 1 times
My Contributions
Trying to Produce the numbers (1-8) randomly and mutually exclusive (getting an endless loop though)
This program produces 7 random Numbers..but when you try to set Redim Number to (7) to receive an eighth index output, it creates an endless loop because arrayused.length gets set to 9 and the Index(8) value is never used and thats a problem which is causing the loop to never exit.
The original program was presented on this website in another lanquage and I have converted it to english. I spent about 5 hours debugging and I just dont have enough brains to figure out how to fix the endless looping that occurs if array is Redimmed to (7)

This randomization needed is intended to help me create an 8-man tournament bracket for Bowling Tournaments.
The original post was submitted by m2s87 in another language (possibly french?)

Someone please help me .. reading tons of books on vb.net lately as my 6 years at tech school has not been enough knowledge yet..

Please feel free to send suggestions.... Thank You
Schmit35@msn.com---IT Graduate 2004 Oregon Institute of Technology

CODE

Public Class Form1
    'Create empty string of 8 numbers
    Dim number(6) As String 'This needs to be (7) but causes an endless loop when changed

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ReadingRandomMutuallyExclusiveArray(number, 8) 'Number of members
    End Sub

    Sub ReadingRandomMutuallyExclusiveArray(ByRef arraytorandomize, ByVal MaxNumber)


        Dim a As String
        Dim arrayused() As String
        Dim i As Integer
        Dim ArrayIndexUsed As Integer


        Randomize()
        ' ReDim Preserve arrayused(7)

        arrayused = Split(GeneratedNumberString(MaxNumber), ",")


        '-----------------------------------------------------------------------------------------------------

        For i = LBound(arraytorandomize) To UBound(arraytorandomize)

            Do
                ArrayIndexUsed = Int(Rnd() * (MaxNumber - 1) + 1) 'ArrayIndexUsed = Random Number Picked

                'DoEvent
            Loop While arrayused(ArrayIndexUsed) < 1

            arrayused(ArrayIndexUsed) = 0 'This sets picked out array value as zero

            arraytorandomize(i) = ArrayIndexUsed 'Massiv is second array

            a = Join(number, ",")  'number is picked out of arrayused and create new list of 8

            TextBox1.Text = a & vbCrLf

        Next i
        Exit Sub

    End Sub

    Function GeneratedNumberString(ByVal RemainingNumberstoRandomize) 'Creates 8,7,6,5,4,3,2,1
        If RemainingNumberstoRandomize > 0 Then GeneratedNumberString = RemainingNumberstoRandomize _
        & "," & GeneratedNumberString(RemainingNumberstoRandomize - 1)
        If RemainingNumberstoRandomize = 1 Then RemainingNumberstoRandomize = 0

    End Function

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        ReadingRandomMutuallyExclusiveArray(number, 8)
    End Sub
End Class




User is offlineProfile CardPM
+Quote Post

RodgerB
RE: Trying To Produce The Numbers (1-8) Randomly And Mutually Exclusive
21 Dec, 2007 - 09:24 PM
Post #2

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
I found this topic quite interesting and decided I'd take a bash at it myself! smile.gif

Here is a snippet I have created for you, I decided to do it from scratch, so it does look a little different from your previous code:

CODE

Dim intArray(7) As Integer ' Our random array of integers.
Dim rndInst As New Random() ' Our random instance.

' Loop through all elements in the integer array.
For i As Integer = 0 To 7
    ' Create a GoTo point when it creats a random number
    ' that isn't mutually exclusive.
GoAgain:
    ' Give the array number a value so we can check it
    intArray(i) = rndInst.Next(0, 8) + 1
    
    ' Check if there are any other arrays with the
    ' newly created random value.
    For j As Integer = 0 To 7
        ' Ignore the array that we just assigned a value to.
        If j = i Then Continue For
        
        ' If not mutually exclusive, this will be true.
        If intArray(i) = intArray(j) Then GoTo GoAgain
    Next
    
    ' A number has been born! :D
Next


If you'd like to get a string value of all the array elements, here is an example with a MessageBox. Copy and paste it after the above snippet.

CODE

' Our Resulting String
Dim stringResult As String = ""

' Loop through all elements of the intArray
For i As Integer = 0 To 7
    ' If it's not the last one we will put
    ' a comma on the end, just to make it
    ' look snazzy :)
    If Not i = 7 Then : stringResult += Convert.ToString(intArray(i)) & ","
    Else : stringResult += Convert.ToString(intArray(i))
    End If
Next

' Show a message box displaying the resulting string.
MessageBox.Show(stringResult)


Have fun! biggrin.gif
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Trying To Produce The Numbers (1-8) Randomly And Mutually Exclusive
22 Dec, 2007 - 04:38 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,281



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Perhaps I'm missing something. Rather than saying "mutually exclusive" aren't your really just asking for numbers 1-8 in a random order? If so, the logic becomes very simple.

Populate an array with all the numbers you want. Pick a random position in the array and return that value. Shrink the array you're picking from and repeat.

Shrinking here is a little quirky because we don't have to worry about the order of our source set. We simply replace the value in the position we just picked from with the value in the last position. We can repeat this process until we only have one item left in our source, then just pick that one as our final value.

CODE

Function GetNumberString(ByVal listSize As Integer) As String
    Dim seedArray(listSize) As Integer
    Dim rndArray(listSize) As Integer
    Dim rndInst As New Random()
    Dim rndPos As Integer
    Dim lastPos As Integer

    For i As Integer = 0 To listSize - 1
        seedArray(i) = i + 1
    Next

    For i As Integer = 0 To listSize - 2
        lastPos = (listSize - 1) - i
        rndPos = rndInst.Next(0, lastPos)
        rndArray(i) = seedArray(rndPos)
        seedArray(rndPos) = seedArray(listSize-i)
    Next
    rndArray(listSize - 1) = seedArray(0)

    Dim txtOut As String = ""
    For i As Integer = 0 To listSize - 1
        If Not i = 0 Then
            txtOut += ", "
        End If
        txtOut += Convert.ToString(rndArray(i))
    Next
    
    Return txtOut
End Function

TextBox1.Text = GetNumberString(8)


You can also do this with a single array and swapping, but this seemed more straight forward.

Hope this helps.

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 05:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month