Help...Arrays

solon
03-07-2004, 01:13 AM
Here is some code that I have written, that I wish to use as a random name generator. I would like the program to pick a name at random, then remove it from the array so that it cant be chosen again until all the others have been drawn.

Private Sub Form_KeyDown(keycode As Integer, shift As Integer)
random
End Sub
Private Sub random()
Dim max As Integer
Dim term As Integer
Dim temp As Integer
Dim x(12) As Variant
x(0) = "Huy"
x(1) = "Fershad"
x(2) = "James"
x(3) = "Matthew"
x(4) = "Rudolf"
x(5) = "Tim"
x(6) = "Steve"
x(7) = "David"
x(8) = "Martin"
x(9) = "John"
x(10) = "John"
x(11) = "Daniel"
x(12) = "Nathan"

'max = (x.Count - 1)
' Could use that one!
max = (x - 1)
While (max >= 0)
term = (Rnd * max)
temp = x(max) = x(term)
x(term) = temp
max -1
Wend
End Sub

I can't work out why it won't work, but seeing as though I am a Java programmer, I really don't have much hope!

Please help me "clean up" my code!

Thanks, Solon

BryanN
03-07-2004, 01:21 AM
In Form_Load, put the statement
Randomize
this iwll mkae a random number. for the names, you may want to simply use a listbox.

solon
03-07-2004, 01:37 AM
I understand that, but thanksyou.

The problem lies in the line

max = (x - 1)

Well the program wont compile and i think there MAY be problems after that but it hanst picked them up yet, it is stuck on that one!

It says:

"Type Mismatch"

Thanks...

thingimijig
03-07-2004, 03:18 AM
have a look at billsoos tutorial on random numbers here (http://www.visualbasicforum.com/showthread.php?t=76270)

thingimijig.

Mathijsken
03-07-2004, 05:03 AM
I can see that you're not used to VB at all, fyi VB doens't support short-notations like max =- 1 or other java-stuff VB is a bit lame in comparison with Java :p
I've re-written your code a bit:



Option Explicit
'This is a 'home-made'-type, like a string or variant. The reason i use this,
' is that i don't have to declare to arrays: Chosen() and Names(), but an array
' of this private type
Private Type Names
Name As String
Chosen As Boolean
End Type
Const NrOfNames As Integer = 12
Dim X(NrOfNames) As Names
Dim strRandom(NrOfNames) As String 'This will contain all the names in a random order.
Private Sub random()
Dim intTemp As Integer, i As Integer
X(0).Name = "Huy"
X(1).Name = "Fershad"
X(2).Name = "James"
X(3).Name = "Matthew"
X(4).Name = "Rudolf"
X(5).Name = "Tim"
X(6).Name = "Steve"
X(7).Name = "David"
X(8).Name = "Martin"
X(9).Name = "John"
X(10).Name = "John"
X(11).Name = "Daniel"
X(12).Name = "Nathan"

For i = 0 To NrOfNames
Do While strRandom(i) = ""

Randomize
intTemp = Int(Rnd * NrOfNames)

If X(intTemp).Chosen = False Then
strRandom(i) = X(intTemp).Name
X(intTemp).Chosen = True
End If
DoEvents
Loop
Next i
End Sub


The idea is:
1) You loop through a new array of names (strRandom). We'll put our names, randomly selected in this one.
2) You get a random index of the original X array and check wether this one has been chosen before or not. If he has been chosen before pick another random number and another,... untill we get one that hasn't been chosen yet. Then we simply pass the found name to the new array (strRandom).

I don't know what has to happen with the names, tell me and I'l explain you how to acces the random array,or find out yourself, that's up to you I guess ;)

Mathijsken

tinyjack
03-07-2004, 05:35 AM
Your initial problem was that arrays in VB do not have a Count property, you need to use the UBound function, so:


max = (x.Count - 1)

'becomes
max = UBound(x)


HTH

TJ

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum