[NTG-context] A macro which gives a random name

Wolfgang Schuster schuster.wolfgang at gmail.com
Sat Apr 18 19:23:48 CEST 2015


> Am 18.04.2015 um 18:55 schrieb Otared Kavian <otared at gmail.com>:
> 
> Hi everyone,
> 
> In the example below I define a macro which chooses at random a name from a list of names. But I wonder whether this can be done in a more clever way without using a numerical macro created with math.random in Lua. The shortcoming of the macro below is that before hand I must know the nomber of elements in the list of names (for instance 5 in the example below), while it may happen that I need to create as many as random names that there are elements in the list, but sometimes I don’t know what is this number.
> 
> Thanks for any insight and help.
> Best regards: OK
> %%%% begin random-names.tex
> \setuprandomize[2015] % set a seed
> 
> \starttext
> 
> \startluacode
> 	Name = {'F', 'G', 'u', 'v', 'W'}
> \stopluacode
> 
> \define[3]\RandomName{%
> 	\setevalue{Named#1}{\ctxlua{tex.print(math.random(#2,#3))}}}
> \define\RandomFunctionName{\ctxlua{tex.print(Name[\NamedFunctionNumber])}}
> 
> \dorecurse{10}{\RandomName{FunctionNumber}{1}{5}%
> Give an example of a function $\RandomFunctionName : {\Bbb R} \longrightarrow {\Bbb R}$ which has a derivative only at the origin, and such that $\RandomFunctionName(0) = 1$.\par \hairline\par}
> 
> \stoptext
> %%%% begin random-names.tex

You can access the size of your Name table with #Name but have to replace # with \letterhash when you use it in a TeX command because # is already taken for the TeX arguments.

\starttext

\startluacode
	Name = {'F', 'G', 'u', 'v', 'W'}
\stopluacode

\define\RandomFunctionName
  {\startlua
   local listsize    = \letterhash Name ;
   local randomvalue = math.random(1,listsize) ;
   context(Name[randomvalue])
   \stoplua}

\dorecurse{10}{Give an example of a function $\RandomFunctionName : {\Bbb R} \longrightarrow {\Bbb R}$ which has a derivative only at the origin, and such that $\RandomFunctionName(0) = 1$.\par \hairline\par}

\stoptext

Wolfgang



More information about the ntg-context mailing list