Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 131,681 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,482 people online right now. Registration is fast and FREE... Join Now!




Random data

 
Reply to this topicStart new topic

Random data, Visual Basic .NET 2008 Express

south73paw
post 14 Oct, 2008 - 08:47 PM
Post #1


New D.I.C Head

*
Joined: 6 Oct, 2008
Posts: 11


My Contributions


I need help with using the random function to distribute data randomly from an array.

I'm not entirely sure if my code below is efficient or not, but I have managed to get names from a 1D array into 2 list boxes. However, it doesn't randomly distribute the names. The names are retrieved from the array and placed into one of 2 list boxes, but they are in the same order in which they are listed in the array!

I've narrowed it down to my line of code that is trying to randomly shuffle names from the array when they are placed into one of two teams - highlighted. Basically, this line doesn't do anything. When it's commented out, the program runs the same as if it was uncommented.

CODE
Private Sub btnCreateTeams_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTeams.Click

        'Variable for random number assign to each item in the array
        Dim Rand As String

        'Initiate the random number generator
        Randomize()

        'Variable to store total names in array
        Dim NoOfNames As Integer
        'Place total number of names currently in the array into variable NoOfNames
        NoOfNames = Names.Length
        'Check that the above line is correct - testing purposes only
        lblNoOfNames.Text = NoOfNames

        'Variable to enable names to be split across two list boxes
        Dim MaxTeamOne As Integer
        'Calculate the max number of names which can go into the first list box
        'Integer division at work
        MaxTeamOne = NoOfNames \ 2


       [color=#009900][b] 'Rand = (Rnd() * Names.Length - 1)[/b][/color]

        'Loop until all names in the array have been read
        For i = 0 To Names.Length - 1
            'Read one name at a time and place in Rand variable
            Rand = Names(i)
            'Limit the number of names in the first list box to half the total number
            'of names currently in the array
            If lstTeamOne.Items.Count <= MaxTeamOne - 1 Then
                'Add names from array to first list box
                lstTeamOne.Items.Add(Rand)
            Else
                'Add names from array to second list box
                'when the first list box is deemed to be full
                lstTeamTwo.Items.Add(Rand)
            End If
        Next i
        'When all names from the array have been read into the list boxes
        If lstTeamTwo.Items.Count = MaxTeamOne + 1 Then
            'Display this message to user
            MessageBox.Show("Teams allocated.  Enjoy the game!")
            'Disable the button that runs this code's click event
            btnCreateTeams.Enabled = False
        End If

    End Sub


What am I doing wrong for the random side of things not to work? I'd really appreciate any help people could give.
Thanks

This post has been edited by south73paw: 14 Oct, 2008 - 08:50 PM
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 14 Oct, 2008 - 09:17 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,008



Thanked 170 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well you setup the Rand variable, but then you overwrite it from the names array. Why? That is your problem...

vb

For i = 0 To Names.Length - 1
' Problem here, you read a name from the list in order and give it
' to Rand, but Rand was your random number
' (which means it should be an integer first of all)
Rand = Names(i)


So if you setup Rand as an integer, assign a random number based on the size of your names list... aka Rand = (Rnd() * (Names.Length - 1)). So if you can pick random names out of the array and place them back and forth between the two lists.

The new trick will be to determine if a name has already been selected from the list and to stop as soon as the list has been exhausted of names.

Just to give you an idea of how you can randomly pick names from an array you can take a look at this example I wrote up for you...

vb

' Array of Names
Dim Names(4) As String

Names(0) = "Tom"
Names(1) = "Martin"
Names(2) = "Mary"
Names(3) = "James"
Names(4) = "John"

' Random number which will be between 0 and 4 subscripts of the array
Dim Rand As Integer
Rand = (Rnd() * (Names.Length - 1))

' Show the name of the random name chosen
MessageBox.Show(Names(Rand))


Here above you will have a messagebox that will show a name from the array at random. You can simply go back and forth between your two lists instead until all names from the array have been added.

How you mark off if a name has been selected is up to you. Some choices are making it a two dimensional array where the second dimension is a flag saying it has been chosen, another way is to add the name to a list of choices and once the list has as many names as the array's size, you know you have them all. Plus there are a few other ways. It is up to you and your design.

But this is a way to get your randomness and you can easily split using it. Hope it helps you out.

"At DIC we be name splitting code ninjas... we also split wood, bananas, and split from situations when two chicas get in a fight over who's man is a better father to little Enrique." decap.gif
User is offlineProfile CardPM

Go to the top of the page

south73paw
post 15 Oct, 2008 - 01:28 AM
Post #3


New D.I.C Head

*
Joined: 6 Oct, 2008
Posts: 11


My Contributions


Thanks, Martyr2.

Here are some of the variations I've come up with.

This takes names from the array in random sequence, but displays this same sequence in both list boxes:

CODE
    For i = 0 To Names.Length - 1

            Rand = (Rnd() * (Names.Length - 1))

            lstTeamOne.Items.Add(Names(Rand))

            If lstNames.Items.Count > MaxTeamOne - 1 Then

                lstTeamTwo.Items.Add(Names(Rand))

            End If

        Next i


This one's closer to what I want to achieve:
CODE
    
Dim Names() As String

Dim Rand As Integer

   'Initiate the random number generator
        Randomize()

'Variable to enable names to be split across two list boxes
        Dim MaxTeamOne As Integer

        'Calculate the max number of names which can go into the first list box
        'Integer division at work
        MaxTeamOne = (Names.Length - 1) \ 2

Rand = (Rnd() * (Names.Length - 1))

        For i = 0 To Names.Length - 1

            If lstTeamOne.Items.Count <= MaxTeamOne - 1 Then

                lstTeamOne.Items.Add(Names(Rand))

            Else

                If lstTeamOne.Items.Count > MaxTeamOne - 1 Then

                    lstTeamTwo.Items.Add(Names(Rand))

                End If

            End If

        Next i

        'When all names from the array have been read into the list boxes
        If lstTeamTwo.Items.Count = MaxTeamOne + 1 Then

            'Display this message to user
            MessageBox.Show("Teams allocated.  Enjoy the game!")

            'Disable the button that runs this code's click event
            btnCreateTeams.Enabled = False

        End If


BUT - while, from an array with 7 names I get 3 names in the first list box and 4 names in the 2nd list box, it's the same name repeated. Grabbing a name from the array at random is now working (thanks!), but why does it repeat when I put things in a loop?

The closest I've managed to get is having a single name added to a list box on separate clicks on the button - hence the need for a loop, isn't it?

I hope that makes sense!

I've been trying things out for ages and it's all starting to look the same....

I haven't got my head around validating if the name has already been taken from the array yet - I'll get there...
User is offlineProfile CardPM

Go to the top of the page

dbasnett
post 15 Oct, 2008 - 11:21 AM
Post #4


D.I.C Head

**
Joined: 1 Oct, 2008
Posts: 131



Thanked 5 times
My Contributions


CODE
        Dim myRandom As New Random, idx As Integer
        Dim Names As New List(Of String)
        Dim team1 As New List(Of String)
        Dim team2 As New List(Of String)

        Names.Add("Tom")
        Names.Add("Martin")
        Names.Add("Mary")
        Names.Add("James")
        Names.Add("John")
        Names.Add("Tom2")
        Names.Add("Martin2")
        Names.Add("Mary2")
        Names.Add("James2")
        Names.Add("John2")
        Names.Add("Tom3")
        Names.Add("Martin3")
        Names.Add("Mary3")
        Names.Add("James3")
        Names.Add("John3")

        Do While Names.Count > 0
            idx = myRandom.Next(0, Names.Count)
            team1.Add(Names.Item(idx))
            Names.RemoveAt(idx)
            If Names.Count = 0 Then Exit Do
            idx = myRandom.Next(0, Names.Count)
            team2.Add(Names.Item(idx))
            Names.RemoveAt(idx)
        Loop
        ListBox1.Items.AddRange(team1.ToArray)
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 07:48AM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month