4DToday.com
Database Indexing Class
Home Directory Classifieds Reference Archives
Site Info
About 4DToday.com
Contact Us
Version française
Quote
Developers (695)
4D Developers (575)
Web Developers (320)
Tools Publishers (187)
Instructors (162)
Web Hosts (54)
Resellers (80)
Software Publishers (285)
Search Developers
Products (416)
Plug-Ins (125)
Components (61)
Code Libraries (9)
Books (9)
Tools (42)
Vertical Applications (144)
Search Products
User Groups
All User Groups (20)
Classified Ads (4)
Help Wanted (1)
Opportunities Wanted (2)
Hardware Wanted (0)
Hardware for Sale (0)
Software Wanted (0)
Software for Sale (0)
Other (1)
Resources
4D
4D, Inc.
4D International
4D Wiki
4D Partner Central
4D Beginner's Center
4D Online Documentation
4D 2004 Example Applications
4D Example Databases
4D BugDisplayer
4D, Inc. Training
Message Lists
4D Mailing Lists
4D Tech Google Group
iNUG Archives (Nabble)
iNUG Archives (Gmane)
iNUG Message Index
Other
4D Code Exchange
4D Resources
Sviluppo4D.it (Italy)
Quote
Patience is also a form of action.
Auguste Rodin  
  Suggest a Quote  
Developer News
4D v11 SQL Road Show
4D, Inc. has announced the 4D v11 SQL Road Show. The tour includes stops in seven U.S. cities. The morning session is free, followed by an afternoon training session priced at $299 ($199 for Partners). 8:45 AM PST
Minneapolis Area 4D Users Group Meeting on May 19th
The next Minneapolis area 4D users group meeting will be held on Monday, May 19th at 7 PM. 12 May 2008
 Submit Future News    Archive 
Technical Tip
Getting a Random Selection of Records
Submitted by Brian Campbell, PetData Inc.

There are several  reasons that you may want to get a random selection of records from a particular table.  The following method does just that. Note that it uses another method number_RandomLong, published previously.

There are two different 'methods' for getting the selection. Which one you use will depend on your application. The 'Array' method is better for larger result sets, but may stall somewhat with very large starting selections. The 'Sets' method is not affected by the size of the source selection, but has a greater overhead and slows as your result set size increases. Do some testing and determine which works best for your purposes.


  ` GetRandomSelection (SourceTable;RecordCountToGet)
  `
  ` Makes a random selection of records in the source table
  ` Uses the current selection as a starting point
  ` If starting selection is same or less than record count to get, the current
  ` selection of the table is not changed
  `
  ` $1 - $sourceTable_ptr - pointer to the table to work on
  ` $2 - $recCountToGet - longint - total number of records in resulting selection
  ` $0 - <none>

C_POINTER($1;$sourceTable_ptr)
C_LONGINT($2;$recCountToGet)
$sourceTable_ptr:=$1
$recCountToGet:=$2

C_BOOLEAN($useArrays)
$useArrays:=True
  ` You may wish to establish some sort of criteria to determine which method
  ` is used, such as the specific table or the size of the source/result selections.
  ` This could also be added as a third optional parameter with a default.

C_LONGINT($startRecordCount)
$startRecordCount:=Records in selection($sourceTable_ptr->)

If ($startRecordCount > $recCountToGet)  ` Make sure we have enough source records
 
  If ($useArrays)
      ` This method uses arrays and record ids
   
      ` Setup source and result arrays   
    ARRAY LONGINT($allRecordIDs_aL;0)  ` Source
    LONGINT ARRAY FROM SELECTION($sourceTable_ptr->;$allRecordIDs_aL)
    ARRAY LONGINT($resultRecIDs_aL;$recCountToGet)  ` Result
   
    C_BOOLEAN($foundUnique)
    C_LONGINT($rec;$thisRecID;$resultIndex;$randomIndex)
   
    For ($rec;1;$recCountToGet)
      $foundUnique:=False  ` Set true when we know we have a unique rec ID
      Repeat
          ` Pick a random rec ID       
        $randomIndex:=number_RandomLong (1;$startRecordCount)
        $thisRecID:=$allRecordIDs_aL{$randomIndex}
          ` Check for uniqueness
        If ((Find in array($resultRecIDs_aL;$thisRecID))=-1)  ` Not yet in result array
          $foundUnique:=True
        End if
      Until ($foundUnique)
      $resultRecIDs_aL{$rec}:=$thisRecID  ` Add this rec ID to the result array
    End for
   
      ` Change the selection using the result array of rec IDs   
    CREATE SELECTION FROM ARRAY($sourceTable_ptr->;$resultRecIDs_aL)
   
  Else
      ` This method simply builds a set with goto selected record   
    C_TEXT($randomSetName)
    $randomSetName:="RandomResultSet"
    CREATE EMPTY SET($sourceTable_ptr->;$randomSetName)
   
    Repeat
        ` Goto a random record number in the selection     
      GOTO SELECTED RECORD($sourceTable_ptr->;number_RandomLong (1; $startRecordCount))
        ` Add this record to the set   
      ADD TO SET($sourceTable_ptr->;$randomSetName)
    Until (Records in set($randomSetName)=$recCountToGet)
   
      ` Change the selection and clear the set
    USE SET($randomSetName)
    CLEAR SET($randomSetName)
   
  End if   ` which method to use
 
End if   `(Records in selection($sourceTable_ptr->) > $recCountToGet)
  Submit a Tip    Archive 
Spotlight
10 Big Myths about Copyright
10 Big Myths about Copyright We recently ran across an interesting article we thought we'd share with you. Brad Templeton's 10 Big Myths about Copyright does a good job of explaining some of the gray areas about copyright law.
  Suggest a Spotlight  
Survey Question
Today, we ask you...
Do you use splitters on your forms?
Often
Sometimes
Rarely
Never
  Show Results (no vote)    Suggest a Survey  
Events
May 2007
19 Minneapolis 4D SIG Meeting
In Maple Grove from 7:00 PM to 9:00 PM
Details
  Submit an Event