PrG_NonRandom_Random.xml 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
  4. <sect1 id="Non-Random_RANDOM">
  5. <title><emphasis role="bold">Non-Random RANDOM</emphasis></title>
  6. <para>There are occasions when you need a random number, but once
  7. you've gotten it, you want that value to stay the same for the duration of
  8. the workunit. For example, the “problem” with this code is that it will
  9. OUTPUT three different values (this code is in
  10. NonRandomRandom.ECL):</para>
  11. <programlisting>INTEGER1 Rand1 := (RANDOM() % 25) + 1;
  12. OUTPUT(Rand1);
  13. OUTPUT(Rand1);
  14. OUTPUT(Rand1);</programlisting>
  15. <para>To make the “random” value persistent throughout the workunit, you can
  16. simply add the STORED Workflow Service to the attribute definition, like
  17. this (this code is also in NonRandomRandom.ECL):</para>
  18. <programlisting>INTEGER1 Rand2 :<guimenu>=</guimenu> (RANDOM() % 25) + 1 : STORED('MyRandomValue');
  19. OUTPUT(Rand2);
  20. OUTPUT(Rand2);
  21. OUTPUT(Rand2);</programlisting>
  22. <para>This will cause the “random” value to be calculated once, then used
  23. throughout the rest of the workunit.</para>
  24. <para>The GLOBAL Workflow Service would accomplish the same thing, but using
  25. STORED has the added advantage that the “random” value used for the workunit
  26. is displayed on the ECL Watch page for that workunit, allowing you to better
  27. debug your code by seeing exactly what “random” value was used for the
  28. job.</para>
  29. </sect1>