|
-
Feb 18th, 2006, 12:12 PM
#1
Thread Starter
Member
Searching an Array?
Ok, basically I've created a random number which is stored in Index and this value is then added as the first value in an array. This process is repeated of making a random index number then storing it in the next value of an array.
I have managed to do this, but I dont know how to search the array full of index values to see if when a random number has been created if that number is in the array then a new random number has to be generated.
I already have some of the code for this bit,
VB Code:
Randomize
Index = Int(Rnd * 10) + 3
Do While *the value index is in the array*
Index = Int(Rnd * 10) + 3
Loop
The array is called QuestionsDone(13)
Can anyone help me?
Thanks.
-
Feb 18th, 2006, 12:29 PM
#2
Re: Searching an Array?
You can loop the array to check if the number is already generated, however this will take time especially at the end of the array since there is only one number left that can be generated and you might need to randomly select many numbers before this is done. I can show you an easier way of creating an array with random numbers, but I'll need a bit more information before I do that since I'm a bit confused by your code.
You say that you have an array named QuestionsDone(13) which (unless you have set Option Base 1 at the top of your code) means that you have 14 indexies in that array, QuestionsDone(0) To QuestionsDone(13). However the random number you generate with this code:
VB Code:
Index = Int(Rnd * 10) + 3
will get a random number between 3 and 12 (a total of 10 different numbers can be generated here). So how can you fill an array with 14 items with 10 different numbers and you don't want any duplicates?
-
Feb 18th, 2006, 03:00 PM
#3
Thread Starter
Member
Re: Searching an Array?
yes you're right it should be QuestionsDone(11) but that was because I was had changed bits of my code it doesn't really matter.
Basically what I'm doing is creating a random number with index because each value of index holds a different question. So therefore when each random index value is created a random question is called. Dont ask why index holds the question, it would make sense if you saw my program just accept it.
But of course I dont want the same question being repeated, so I thought putting the index values in an array would mean that next time round if that number was generated then the array would be searched to see if that number was already there. If it has then a new index value is created until the value is not found in the array.
Is there a better way of doing this?
Thanks in advance.
-
Feb 18th, 2006, 03:12 PM
#4
Re: Searching an Array?
You can use Filter() to find matches. But it must be an Array of strings..
Syntax:
Filter(SourceArray, Match, [include] as Boolean, Compare Method)
Example on how to use Filer..
This use 2 comandbuttons and 2 Listsboxes.
The listbox is filled with even numbers, so select a number from list1 and click command1, it will show in list2 all matches, command2 will show all 'no matches' of the number you selected.
VB Code:
Private StArray() As String
Private StTempArray() As String
Private Sub Form_Load()
Dim i As Long
For i = 1 To 40
ReDim Preserve StArray(i)
StArray(i) = CStr(i * 2)
List1.AddItem CStr(i)
Next
End Sub
Private Sub subSearch(pVal As Boolean)
Dim i As Long
If List1.ListIndex = -1 Then Exit Sub
'3rd parameter determines matches or no matches
StTempArray = Filter(StArray, List1.List(List1.ListIndex), pVal)
List2.Clear
For i = 0 To UBound(StTempArray)
List2.AddItem StTempArray(i)
Next
Erase StTempArray
End Sub
Private Sub Command1_Click()
subSearch True
End Sub
Private Sub Command2_Click()
subSearch False
End Sub
EDIT: If you have your questions in an array, you don't even need to worry about indexes, you can use filter() with the question itself, searching the array of 'questions done' to know if it is already there.
Last edited by jcis; Feb 18th, 2006 at 03:32 PM.
-
Feb 18th, 2006, 03:25 PM
#5
Re: Searching an Array?
I'm still confused... How many questions do you have and what's the range of the numbers you want to store? Are there 10, 11 or 12 questions? Are the questions numbered started from 1 or a higher number?
Anyway... The following will return an array (0 To 9) containing the numbers 1 to 10 in a random order.
VB Code:
Public Function GetRandomArray() As Integer()
ReDim iArr(9) As Integer
Dim iTemp As Integer
Dim iRand As Integer
Dim i As Integer
Randomize
'save the numbers 1 through 10
For i = 0 To 9
iArr(i) = i + 1
Next
'now scramble up the values
For i = 0 To 9
Do
iRand = Int(Rnd * 10)
Loop While iRand <> i
'Swap the values in the array
iTemp = iArr(iRand)
iArr(iRand) = iArr(i)
iArr(i) = iTemp
Next
'Return the array
GetRandomArray = iArr
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|