Results 1 to 18 of 18

Thread: [RESOLVED] randomly selecting number then excluding it

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    Wauchope, NSW, Australia
    Posts
    92

    Resolved [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...

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    Wauchope, NSW, Australia
    Posts
    92

    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)

  3. #3
    Lively Member
    Join Date
    Mar 2005
    Posts
    103

    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

  4. #4
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    Wauchope, NSW, Australia
    Posts
    92

    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)

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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:
    1. Public Sub Sample()
    2.     Dim lngArray(1 To 25) As Long
    3.     Dim i As Long
    4.    
    5.     For i = LBound(lngArray) To UBound(lngArray)
    6.         lngArray(i) = i
    7.     Next
    8.     ShuffleArray lngArray
    9.     ' Pick 20 random numbers
    10.     For i = 1 To 20
    11.         Debug.Print lngArray(i)
    12.     Next
    13. End Sub
    Last edited by Ellis Dee; Jul 24th, 2007 at 07:46 PM.

  7. #7
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    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().

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    Wauchope, NSW, Australia
    Posts
    92

    Arrow and then...??

    thanks heaps for that!

    like this? (listbox is just to view result)...

    then how would i load each question (form) in that shuffled order? (show next form after user submits answer on previous form)
    Attached Files Attached Files
    Last edited by AC_AC_AC187; Aug 26th, 2007 at 05:32 AM. Reason: no reason

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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?

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    Wauchope, NSW, Australia
    Posts
    92

    Re: randomly selecting number then excluding it

    it's a project for school and that's the way it's gotta be done...

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    Wauchope, NSW, Australia
    Posts
    92

    Lightbulb 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

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    Wauchope, NSW, Australia
    Posts
    92

    Exclamation 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.
    Attached Files Attached Files

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    Wauchope, NSW, Australia
    Posts
    92

    Exclamation 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...
    Attached Files Attached Files

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    Wauchope, NSW, Australia
    Posts
    92

    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...

  16. #16
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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?

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    Wauchope, NSW, Australia
    Posts
    92

    Re: randomly selecting number then excluding it

    yes please, i really need to get this working before tomorrow if possible...

  18. #18
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width