Jump to content

Selecting random records from found set


Recommended Posts

Hi all

I have the current script below set to find a random number of records based on the test question requirement (TestQuestionCount) within a found set. The script successfully is able to isolate x number of questions depending on the TestQuestionCount requirement. I have watched the Data Viewer during script debugging. However, if the question count requirement is 30 questions, it's usually finding ~25 records or so. Upon further investigation I see it is randomly (of course) capturing duplicate record ID's. When the system is creating a new request there are usually a couple duplicates, thus my found set is not matching the TestQuestionCount. The error I believe is happening at the beginning of the first Loop below, but I'm unable to determine where I've gone wrong.

A keen eye appreciated.

 

Set Error Capture [On]
Set Variable [ $maxQuestions ; Value: ACS PTS Type::TestQuestionCount ]
If [ Get (FoundCount ) ≤ $maxQuestions
	Exit Script
End If
# Get a list of randomly selected records from the found set
Loop
	Set Variable [ $n ; Value: Round ( Random * Get (FoundCount) ; 0 ) + 1 ]
    Set Variable [ $id.list ; Value: List ( $id.list ; GetNthRecord ( Questions::PrimaryKey ; $n )) ]
    Omit Record
    Exit Loop If [ ValueCount ( $id.list ) = $maxQuestions or not Get (FoundCount) ]
End Loop
#Find selected records
Enter Find Mode [ Pause: Off ]
Loop
	Set Field [ Questions::PrimaryKey ; "==" & GetValue ( $id.list ; Get (RecordNumber )) ]
    Exit Loop If [ Get (RequestCount ) = $maxQuestions ]
    New Record/Request
End Loop
Perform Find [ ]

 

Link to post
Share on other sites

The problem is that you are omitting a record, not necessarily the record you have selected. Thus the selected record can remain in the found set and be selected again.

A better method would go to a random record from the current found set*, add its ID to the list and omit it. This eliminates the possibility of selecting the same record twice.

---
(*) See here how:
https://fmforums.com/topic/100245-building-a-quizzing-flashcard-system/?do=findComment&comment=456349

 

  • Like 1
Link to post
Share on other sites
14 minutes ago, comment said:

The problem is that you are omitting a record, not necessarily the record you have selected. Thus the selected record can remain in the found set and be selected again.

A better method would go to a random record from the current found set*, add its ID to the list and omit it. This eliminates the possibility of selecting the same record twice.

---
(*) See here how:
https://fmforums.com/topic/100245-building-a-quizzing-flashcard-system/?do=findComment&comment=456349

 

Thanks, comment. Nailed it.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.