lotus-noteslotus-dominolotusscriptlotus

Questionnaire system random questions


If there are many questions, choose five without repeating How do this? Thanks a lot for help

My GetQuestion Code

Public Function GetQuestion( Byval QuestionNumber )
On Error Resume Next    
Dim MyUIWorkspace As New NotesUIWorkspace
Dim MyDoc As NotesDocument
Set MyDoc = MyUIWorkspace.CurrentDocument.Document

Dim Question
Dim LineNo
LineNo = 0
Forall Tmp In MyDoc.GetFirstItem( "Questions" ).Values
    If LineNo = QuestionNumber Then Question = Tmp
    LineNo = LineNo +1
End Forall
LineNo = 0
Forall Tmp In MyDoc.GetFirstItem( "Answers" ).Values
    If LineNo = QuestionNumber Then Answers = Tmp
    LineNo = LineNo +1
End Forall

Dim MyVar() As Variant
Redim MyVar(1)
MyVar(0) = Question
MyVar(1) = Answers

GetQuestion = MyVar

End Function

View enter image description here


Solution

  • Create a function to get a random number of values from an array...

    %REM
      WARNING: Modifies array argument
      n must be between <code>1</code> and < code>UBound(array) + 1</code>
    %END REM
    Function GetNRandomFromArray(array As Variant, ByVal n As Integer)
      Dim aRet(n - 1) As Variant
      Dim r as Integer
      Dim u as Integer
    
      u = UBound(array)
      For iRet = 0 To n - 1
        iFrom = Int(Rnd * (u + 1)) 'Random number from 0 to u
        aRet(iRet) = array(iFrom)
        If iFrom <> u Then array(iFrom) = array(u)
        u = u - 1 'decrement the number of questions to choose from in the next iteration
      Next iRet
    
      GetNRandomFromArray = aRet
    End Function
    

    And now you can just do

    Dim aRandomQuestions As Variant
    aRandomQuestions = GetNRandomFromArray(MyDoc.Questions, 5)
    

    (Note: MyDoc.Questions and MyDoc.GetFirstItem( "Questions" ).Values both return an array of values that's a snapshot... Changing that array will not automatically affect the values of the field.)