Hiya,
I have 10 people and need to assign 10 random numbers between 1 and 10 for random seating purposes each time.My code so far is below but assigns sometimes the same numbers. How can I make it so that once a number is used it won't be produced for the other seats?

'Pick random numbers to determin where we're all going to sit
Dim LiRandomNumber As Integer
'Player 1
LiRandomNumber = Int(10 * Rnd) + 1
Label42.Caption = LiRandomNumber
'Player 2
LiRandomNumber = Int(10 * Rnd) + 1
Label43.Caption = LiRandomNumber
'Player 3
LiRandomNumber = Int(10 * Rnd) + 1
Label44.Caption = LiRandomNumber
'Player 4
LiRandomNumber = Int(10 * Rnd) + 1
Label45.Caption = LiRandomNumber
'Player 5
LiRandomNumber = Int(10 * Rnd) + 1
Label46.Caption = LiRandomNumber
'Player 6
LiRandomNumber = Int(10 * Rnd) + 1
Label47.Caption = LiRandomNumber
'Player 7
LiRandomNumber = Int(10 * Rnd) + 1
Label48.Caption = LiRandomNumber
'Player 8
LiRandomNumber = Int(10 * Rnd) + 1
Label49.Caption = LiRandomNumber
'Player 9
LiRandomNumber = Int(10 * Rnd) + 1
Label50.Caption = LiRandomNumber
'Player 10
LiRandomNumber = Int(10 * Rnd) + 1
Label51.Caption = LiRandomNumber

you could use loops!

one way would be to loop all assigned players when you generate a new number, making sure nbody else has been assigned that number yet. putting this whole logic in an outter loop to start over if it has.

but that is not the most efficient …

I would do it similar to this...

``````Dim LiRandomNumber As Integer, xRandom As Integer, xList As Integer, xLabels As Integer

For xRandom = 1 To 10
LiRandomNumber = Int(10 * Rnd) + 1

lstNumbers.ListIndex = 0 ''Add a ListBox with its visible property set to False...

For …``````

Here's a little simpler way, all you need is an array of 10 labels(Label1) and a command button(Command1). Each click of the button produces a new table arrangement:

``````Dim Names() As String
Dim NameString As String
Dim Seat As Integer
Dim Table As String

Private Sub Command1_Click() …``````

Did you change your labels (Label42, Label43,... Label51) to be within an array (Label42 as the name with all of its indexes from 0 to 9)?

## All 14 Replies

you could use loops!

one way would be to loop all assigned players when you generate a new number, making sure nbody else has been assigned that number yet. putting this whole logic in an outter loop to start over if it has.

but that is not the most efficient way to do it. what would probly be quicker for you is to generate an array of 10 ints and store in it numbers from 1 to 10. then randomize the array, and just assign players the numbers in the array as they come.

I would do it similar to this...

``````Dim LiRandomNumber As Integer, xRandom As Integer, xList As Integer, xLabels As Integer

For xRandom = 1 To 10
LiRandomNumber = Int(10 * Rnd) + 1

lstNumbers.ListIndex = 0 ''Add a ListBox with its visible property set to False...

For xList = 0 To lstNumbers.ListCount - 1
lstNumbers.ListIndex = xList

If lstNumbers.Text = LiRandomNumber Then

LiRandomNumber = Int(10 * Rnd) + 1

End If
Next xList

Next xRandom

''Change your labels to an array - Label42(0) - Label42(9)

lstNumbers.ListIndex = 0

For xLabels = 0 To lstNumbers.ListCount - 1
lstNumbers.ListIndex = xLabels
Label42(xLabels).Caption = lstNumbers.Text
Next xLabels
``````

Thanks fella. There seems to be an error on line:

Label42(xLabels).Caption = lstNumbers.Text

is the syntax correct?

Here's a little simpler way, all you need is an array of 10 labels(Label1) and a command button(Command1). Each click of the button produces a new table arrangement:

``````Dim Names() As String
Dim NameString As String
Dim Seat As Integer
Dim Table As String

Private Sub Command1_Click()
ResetTable
'loop through the labels to assign names
For Each lbl In Label1
'keep looping until we get a unique name to assign to this label
While lbl.Caption = ""
Seat = Int(10 * Rnd)
'Search the Table string to see if this name is already assigned
If InStr(Table, Names(Seat)) = 0 Then
lbl.Caption = Names(Seat)
'add each name to the table string to use the built in instr function
Table = Table + Names(Seat) + "/"
End If
Wend
Next
End Sub
Private Sub ResetTable()
For Each lbl In Label1
lbl.Caption = ""
Next
Table = ""
End Sub
ResetTable
'initialize Names array any names will do just make sure you use the delimeter("/")
NameString = "A/B/C/D/E/F/G/H/I/J"
Names = Split(NameString, "/")
End Sub
``````

Did you change your labels (Label42, Label43,... Label51) to be within an array (Label42 as the name with all of its indexes from 0 to 9)?

Hi Guys,

I appreciate your help. For some reason I can't get either code to work. I must admit my VB6 skills are limited.
Tinstaafl, the instruction, 'ResetTable' kept causing an error, seemed like it was looking for a procedure with the same name.
AndreRet, I did create an array but it seemed like it was having some sort of trouble. I've not done too much array work so possily me LOL.

Put one label on there it'll be named Label1. adjust any properties that you need to adjust,I'd suggest making the border Fixed Single, then copy the label, and paste a copy onto the form. VB6 will ask you if you want to start an array. When you say yes, each copy you add will be part of the array of labels named Label1. Once you get 10 of them, arrange them as you'd like.

Now add a command button, VB6 will automatically name it Command1.

Delete all the code in the code window. Copy and paste my code, in it's entirety, into the code window of the form. As long you set this up right the code will work. I'd suggest getting it to work as is first, then adapt it to your needs.

Follow what tinstaafl said and also start learning to use array objects and variables. I promise you that you're life will be easier on using array objects ;)

OT: I don't know where to post this but i need your support by voting Visual Basic Classic to be improved: Bring back Classic Visual Basic, an improved version of VB6

thanks,

Bring back Classic Visual Basic, an improved version of VB6

We already have an improved version, it's VB.net. With improved Intellisense, improved documentation(for the most part), and more developer control, the only reason I use VB6 anymore is to help people still using it.

I have another way without using an array. Just randomly switch each players seat with another. Going through each seat, (1-10), switch it with another seat until all seats have been switched at least once.

``````Private Sub btnAssignSeats_Click()
Dim p As Byte           ' Player's current seat number. (Player being assigned)
Dim Seat As Integer     ' Player's New seat assignment.
Dim oPlayer As String   ' Original Player's name.       (Player being displaced)

For p = 1 To 10
' Get a new random seat assignment.
Seat = Int(10 * Rnd) + 1
' Save the original player's name. (The Player being displaced)
oPlayer = Me.Controls("TextBox" & Seat).Text
' Move the 'Assigned' player to their new seat.
Me.Controls("TextBox" & Seat).Text = Me.Controls("TextBox" & p).Text
' Move the 'Original', displaced player to 'Assigned' player's old seat.
Me.Controls("TextBox" & p).Text = oPlayer
Next p
End Sub
``````

I just have a simple form with 10 textboxes, ("TextBox1" -to- "TextBox10"), and 10 Labels to the left of them, with the Seat Number, ("Seat 1" -to- "Seat 10"), and an "Assign Seats" button. This way you can type in each player's name into the text boxes, hit the "Assign Seats" button, and done. If you don't like the assigned seats just hit the button again and it will just keep moving the player's seats.

Sorry about the last post I tried to edit but think I was too late... Just changed the text boxes to an indexed group called "Players" (Indexed: 0 to 9), simplified the form to match my original description and cleaned up the code a bit. The advantage of the code below is it should be able to accomidate any number of seats. Just add or subtract the number of "Players" TextBoxes on the form, no code changes needed.

``````Private Sub btnAssignSeats_Click()
Dim Player As TextBox   ' Player's current seat.        (Player being assigned)
Dim Seat As Integer     ' Player's New seat assignment.
Dim oPlayer As String   ' Original Player's name.       (Player being displaced)

Randomize ' Remember to start the Random number generator.

For Each Player In Players
' Get a new random seat assignment.
Seat = CInt(Int((Players.UBound - Players.LBound + 1) * Rnd() + Players.LBound))
' Save the original player's name. (The Player being displaced)
oPlayer = Players(Seat).Text
' Move the 'Assigned' player to their new seat.
Players(Seat).Text = Players(Player.Index).Text
' Move the 'Original', displaced player to 'Assigned' player's old seat.
Players(Player.Index).Text = oPlayer
Next Player
End Sub
``````

We already have an improved version, it's VB.net. With improved Intellisense, improved documentation(for the most part), and more developer control, the only reason I use VB6 anymore is to help people still using it.

Im sorry but i wont consider VB.NET as the same as VB6. Anyway I just need votes for it so if you really like the classic Visual basic then please do vote.

Thank you,

Fantastic stuff guys! Many thanks, I'd never done array indexing before so learned a lot here.
I decided demon916 example was absolutly what I was looking for, so I've got that working now. Many thanks AndreRet and Tinstaafl too.

Only a pleasure. Happy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.