Difference between revisions of "Command/getrandomnumber"

From Wiki
Jump to navigation Jump to search
m
m
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
 
= Example =
 
= Example =
  
The following example produces a random number between 1 and 10.
+
The following example sets the macro <tt>n</tt> to contain a randomly selected number between 1 and 10.
  
 
<texcode source="yes">
 
<texcode source="yes">
 +
\ctxlua{math.randomseed( os.time() )}
 +
 
\starttext
 
\starttext
 
   \getrandomnumber \n {1} {10}
 
   \getrandomnumber \n {1} {10}
Line 13: Line 15:
 
\stoptext
 
\stoptext
 
</texcode>
 
</texcode>
 +
 +
The first line of the code initializes the random number generator.

Latest revision as of 08:17, 31 March 2014

Overview

The \getrandomnumber command provides a mechanism to create a macro initialized to a random value.

Example

The following example sets the macro n to contain a randomly selected number between 1 and 10.

\ctxlua{math.randomseed( os.time() )}

\starttext
  \getrandomnumber \n {1} {10}
  \n
\stoptext

The first line of the code initializes the random number generator.