12.07.2015 Views

Selecting Random Records from a Found Set - FMWebschool

Selecting Random Records from a Found Set - FMWebschool

Selecting Random Records from a Found Set - FMWebschool

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Selecting</strong> <strong>Random</strong> <strong>Records</strong> <strong>from</strong> a <strong>Found</strong> <strong>Set</strong>So you want to select some random records <strong>from</strong> a found set? This sounds like a job for the<strong>Random</strong> function!According to the Help file, FileMaker's <strong>Random</strong> function returns a random number between zeroand one. What does that mean? Well, that depends on what version of FileMaker you're using.If you go to FileMaker's Knowledgebase article #5464, it tells you that in every version ofFileMaker before 10.0v3, <strong>Random</strong> returns "a random number between zero and one" including"zero and one".However, in FileMaker 10, <strong>Random</strong> was broken on "certain machines": the returned numberwasn't random at all if your computer was one of those "certain machines". 10.0v3 fixed thathowever (make sure you've updated your software, people!).So, back to our initial problem, how do we select random records <strong>from</strong> a found set? You want touse this calculation in the Go to Record/Request/Page script step:Int ( <strong>Random</strong> * Get ( <strong>Found</strong>Count ) ) + 1What this does is gets the count of records in your current found set, multiplies it by a randomnumber, chops off the decimal points, and then adds one.How does this calculation work? Say you have two records in your found set. When you run thiscalculation, if the random number is less than 0.5 (we'll use 0.45 as an example), you will get thefollowing:Int ( 0.45 * 2 ) + 1= Int ( 0.90 ) + 1= 0 + 1= 1So the script step will take you to the first record.If the random number is greater than 0.5 (we'll use 0.55 as an example), you will get thefollowing:Int ( 0.55 * 2 ) + 1= Int ( 1.1 ) + 1= 1 + 1= 2The script step will take you to the second record.In our first example, if you use zero, instead of our sample value of 0.45, the formula still works.If you are using FileMaker 10.0v3, then it is not possible to get one as a random number. Sincethe number is always below one, we don't have to worry about that.


However, if you are using an older version of FileMaker, then you will have to take that intoaccount. As with all calculations in FileMaker, there are multiple ways of solving this problem. Iwill leave it to you to come up with your favourite approach.If you would like to see how I would solve it, please feel free to email me here:paul@paulspafford.com.You can have a look at the sample file (drawNames.fp7) to see how I use this approach to drawnames for prizes at our local user group. Just open the file and click the "Draw a name" function.Paul SpaffordSpafford Data Solutions

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!