PDA

View Full Version : Random Letter Rule Always Returns Same Letter


jleger
January 8th, 2009, 10:02 AM
I have a JavaScript function which returns a random letter, but when calling this from multiple text frames, all frames have the same letter. Refreshing the preview generates a new letter, but again, all frames have the same letter. It's as if the function is only called once even though it should be called by each text frame (in my opinion). Thanks!

----------------------------------------------

randomletters='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
rindex = Math.floor(Math.random() * randomletters.length);
strReturn = randomletters.charAt(rindex);
return strReturn;

----------------------------------------------

Thanks!
-Joe

mhilger
January 9th, 2009, 10:40 AM
Hi Joe,

You assumption is correct - text rules are executed only once per record. So in your job, the rule is executed once and then that the value is returned multiple times in your layout.

One way to achieve what you are after is to have separate rules - 1 for each placement of a different random letter in your template.

You could duplicate the rule you have now, but a cleaner way to do this is to make new rules that simply refer to the base rule you have here. These rules would be:

return Rule("RandomLetterRule");Using this method, you are asking FusionPro to execute this base rule multiple times in a given template.

Now if you want to ensure that the same letter is never returned more than once in a given record, there's a couple options for that. I prefer a method where you do a random sort with a loop on your array in an OnRecordStart rule (so you pick 2 random position in the array and swap the values - repeat this a thousand times and you have a randomized array).

With the randomly sorted array then, you can have rules that return different positions in that array (so rule 1 returns array element 1, rule 2 returns array element 2, etc).

If you'd like some more info on this, please let me know.

hth