[RESOLVED] randomly selecting number then excluding it
am making a multiple-choice question game and want the user to select either a 10 or 20 question game. i should be fine with that much of it, but what i need help with is randomly selecting a number between 1 and 25 (will only choose 10 or 20 of the 25 questions) and then excluding that randomly selected number from being randomly selected again so that question won't be chosen again in the same game...
i was thinking i could have an array containing the numbers 1 to 25 and randomly selecting one of those numbers and loading the question, then setting that chosen number to 0. (and make it so that when randomly selecting a number from the array it has to be greater than 0 and if it's not randomly select another number)
Easiest would be to create an array containing the numbers, shuffle the array so that the numbers are in some random order, then take the first 10 or 20 numbers from the array.
that's a decent idea, how hard is it to shuffle the integers in the array?
(i don't want to randomly generate a number for each cell because i want every number from 1 to 25 to occur once)
This will shuffle any native-type array: (Strings, Longs, etc... Not UDT arrays, sadly.)
Code:
' Knuth shuffle (very fast)
Public Sub ShuffleArray(pvarArray As Variant)
Dim i As Long
Dim iMin As Long
Dim iMax As Long
Dim lngReplace As Long
Dim varSwap As Variant
iMin = LBound(pvarArray)
iMax = UBound(pvarArray)
For i = iMax To iMin + 1 Step -1
lngReplace = Int((i - iMin + 1) * Rnd + iMin)
varSwap = pvarArray(i)
pvarArray(i) = pvarArray(lngReplace)
pvarArray(lngReplace) = varSwap
Next
End Sub
Sample usage:
vb Code:
Public Sub Sample()
Dim lngArray(1 To 25) As Long
Dim i As Long
For i = LBound(lngArray) To UBound(lngArray)
lngArray(i) = i
Next
ShuffleArray lngArray
' Pick 20 random numbers
For i = 1 To 20
Debug.Print lngArray(i)
Next
End Sub
Last edited by Ellis Dee; Jul 24th, 2007 at 07:46 PM.
is it possible to use form names instead of the numbers? (such as frmMain)
and is it possible to load a form from its name in the array?
so that i could do something like this...??
question = 'random form name from shuffled array
me.hide
question.show
Option Explicit
Private Type frm_names
Form_Name As String
Form_Nbr As Integer
End Type
Private Form_Names(1 To 25) As frm_names
Public Sub ShuffleArray(pvarArray As Variant)
Dim i As Long
Dim iMin As Long
Dim iMax As Long
Dim lngReplace As Long
Dim varSwap As Variant
iMin = LBound(pvarArray)
iMax = UBound(pvarArray)
For i = iMax To iMin + 1 Step -1
lngReplace = Int((i - iMin + 1) * Rnd + iMin)
varSwap = pvarArray(i)
pvarArray(i) = pvarArray(lngReplace)
pvarArray(lngReplace) = varSwap
Next
End Sub
Public Sub cmdbtn_Click()
Dim lngArray(1 To 25) As Long
Dim i As Long
lstbox.Clear
For i = LBound(lngArray) To UBound(lngArray)
lngArray(i) = i
Next
ShuffleArray lngArray
For i = 1 To 20
lstbox.AddItem lngArray(i)
Next
End Sub
Private Sub Form_Load()
'Load frm_Names
Form_Names(1).Form_Name = "frmOne"
Form_Names(1).Form_Nbr = 1
Form_Names(1).Form_Name = "frmTwo"
Form_Names(1).Form_Nbr = 2
Form_Names(1).Form_Name = "frmBlah"
Form_Names(1).Form_Nbr = 3
'etc
End Sub
Private Sub lstbox_Click()
Dim lngIndex As Long
Dim frm As Form
For lngIndex = 1 To 25
If Form_Names(lngIndex).Form_Nbr = lstbox.Text Then
Set frm = Forms.Add(Form_Names(lngIndex).Form_Name)
Exit For
End If
Next
frm.Show
End Sub
just figured out that the type mismatch error was from not selecting an item in the listbox before clicking the command button. then it gives me the same variable not set run-time error aswell...
just figured out that the type mismatch error was from not selecting an item in the listbox before clicking the command button. then it gives me the same variable not set run-time error aswell...
Do you need advice on how to stop the error from happening?