KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Generating random test data
PRODUCT: 4D | VERSION: | PLATFORM: Mac & Win
Published On: June 23, 2000

Here is a project method that can be used to fill a test table with random string data. In this example, the database contains a table [SourceStrings] with a single string field "FiftySixChars". Every time the method below is executed, $numCreated random text strings will be created. The random characters are selected from ASCII codes 33 (exclamation point) to 126 (tilde) and include upper and lower case characters and punctuation, but no white space. The resulting random strings are not very legible but can be useful for stress-testing or performance benchmarks where a large number of records are needed.

Cut and Paste the following code example into your
own 4D project

$numCreated:=2000
$stringLength:=56
CONFIRM("Add "+String($numCreated)+" random strings to [SourceStrings]?")
If (OK=1)
 For ($i;1;$numCreated)
  CREATE RECORD([SourceStrings])
  $st:=""
  For ($x;1;$stringLength)
   $st:=$st+Char((Random%(126-33+1))+33)
  End for
  [SourceStrings]FiftySixChars:=$st
  SAVE RECORD([SourceStrings])
 End for
 $message:="Created "+String($numCreated)+" random strings of length "
 $message:=$message+String($stringLength)+" in [SourceStrings]."
 ALERT($message)
End if