[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...
Re: randomly selecting number then excluding it
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)
Re: randomly selecting number then excluding it
yes, have another array bolChoosen(1 to 25)
get intRandom
do
x = intRandom
loop until bolchoosen(x) = false
Re: randomly selecting number then excluding it
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.
Re: randomly selecting number then excluding it
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)
Re: randomly selecting number then excluding it
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
Re: randomly selecting number then excluding it
Note that because shuffling uses random numbers, you should include a Randomize statement in Sub Main() or your startup form's Form_Load().
Re: randomly selecting number then excluding it
Assuming that you forms are named Form1, Form2, Form3, etc...
Code:
Dim frm As Form
Set frm = Forms.Add("Form" & lstbox.Text)
frm.Show
But why do you need one form per question?
Re: randomly selecting number then excluding it
it's a project for school and that's the way it's gotta be done...
Re: randomly selecting number then excluding it
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
Re: randomly selecting number then excluding it
This should work but I haven't tested it.
Code:
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
1 Attachment(s)
Re: randomly selecting number then excluding it
ok, modified the code and am getting a type mismatch run-time error... not sure if i implemented the code correctly?
please help asap, this is due tomorrow afternoon.
1 Attachment(s)
Re: randomly selecting number then excluding it
started a new project again, just copying the code from your last post. this time getting a variable not set run-time error
struggling...
Re: randomly selecting number then excluding it
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...
Re: randomly selecting number then excluding it
Quote:
Originally Posted by AC_AC_AC187
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?
Re: randomly selecting number then excluding it
yes please, i really need to get this working before tomorrow if possible...
Re: randomly selecting number then excluding it
In the command button click event check if Listbox.ListIndex = -1, if it is then theres no item selected so branch processing accordingly or Exit Sub.