Questionnaire system random questions

93 Views Asked by At

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

2

There are 2 best solutions below

9
JSmart523 On BEST ANSWER

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

0
Richard Schwartz On

First count the questions. You've done that already. You have the count in your LineNo variable.

Next, dim an array to hold the questions, and read all the questions into the array. (Note that you can combine these first two steps into one by repeatedly using redim. I tend to not like doing that, but it's perfectly okay.)

Next, dim an array of five integers. and use the Randomize statement to seed LotusScript's random number generator, and dim a list of integers.

Next, initialize a counter to zero and do a while loop that exits when the counter is > 4. In the loop body, use the Int(Rnd() * LineNo) to generate s a random integer in the range 0 to LineNo minus one. If it is in the list, loop around without incrementing the counter. If it isn't in the list, then add it to the list and also use the counter as an index into your array of integers and set that element of the array to your random product, and+ increment the counter.

At this point, you have an array of questions, and you have another array of random indexes into the question array.

Finally, do a loop from 0 to 4. In the loop body, read an index from the array of integers, and use it to read a question from the array of questions.