* A macro which gives a random name @ 2015-04-18 16:55 Otared Kavian 2015-04-18 17:23 ` Wolfgang Schuster 0 siblings, 1 reply; 7+ messages in thread From: Otared Kavian @ 2015-04-18 16:55 UTC (permalink / raw) To: mailing list for ConTeXt users 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 ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: A macro which gives a random name 2015-04-18 16:55 A macro which gives a random name Otared Kavian @ 2015-04-18 17:23 ` Wolfgang Schuster 2015-04-18 19:16 ` Otared Kavian 2015-04-18 20:24 ` Otared Kavian 0 siblings, 2 replies; 7+ messages in thread From: Wolfgang Schuster @ 2015-04-18 17:23 UTC (permalink / raw) To: mailing list for ConTeXt users > Am 18.04.2015 um 18:55 schrieb Otared Kavian <otared@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 ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: A macro which gives a random name 2015-04-18 17:23 ` Wolfgang Schuster @ 2015-04-18 19:16 ` Otared Kavian 2015-04-18 20:24 ` Otared Kavian 1 sibling, 0 replies; 7+ messages in thread From: Otared Kavian @ 2015-04-18 19:16 UTC (permalink / raw) To: mailing list for ConTeXt users [-- Attachment #1.1: Type: text/plain, Size: 3591 bytes --] Hi Wolfgang, Thanks for your attention, but the problem with your solution is that each instance of \RandomFunctionName changes the name chosen, but I need something wihich remains the same name within a given situation (say each problem) but changes from problem to problem. For instance when using your solution with the source code 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$. gives: Give an example of a function 𝐺 : R ⟶ R which has a derivative only at the origin, and such that 𝑊(0) = 1. while what I need is Give an example of a function 𝐺 : R ⟶ R which has a derivative only at the origin, and such that G(0) = 1. (The macro I sent earlier does this, but unfortunately it cannot use the solution you sent). Is there a solution ? Best regards: OK > On 18 Apr 2015, at 19:23, Wolfgang Schuster <schuster.wolfgang@gmail.com> wrote: > > >> Am 18.04.2015 um 18:55 schrieb Otared Kavian <otared@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 > > ___________________________________________________________________________________ > If your question is of interest to others as well, please add an entry to the Wiki! > > maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context > webpage : http://www.pragma-ade.nl / http://tex.aanhet.net > archive : http://foundry.supelec.fr/projects/contextrev/ > wiki : http://contextgarden.net > ___________________________________________________________________________________ [-- Attachment #1.2: Type: text/html, Size: 7135 bytes --] [-- Attachment #2: Type: text/plain, Size: 485 bytes --] ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: A macro which gives a random name 2015-04-18 17:23 ` Wolfgang Schuster 2015-04-18 19:16 ` Otared Kavian @ 2015-04-18 20:24 ` Otared Kavian 2015-04-18 20:36 ` Hans Hagen 1 sibling, 1 reply; 7+ messages in thread From: Otared Kavian @ 2015-04-18 20:24 UTC (permalink / raw) To: mailing list for ConTeXt users Hi again Wolfgang, Thanks to your hint, I could solve the problem… In case someone else would encounter a similar problem to solve, below is a macro which chooses an element from a list, it creates a control sequence (CS) containing that element and it keeps the CS until the next time the macro is invoked again to choose another element. Thanks again and best regards: OK %%%% begin choose-element.tex \setuprandomize[1989] % set a seed \starttext % here is a list from which a name is chosen \startluacode ListOfNames = {'F', 'G', 'u', 'v', 'W'} \stopluacode % this macro has two arguments: % the first argument is the control sequence name attached to Chosen, % the second argument is the name of the list from which something is chosen \define[2]\RandomChoice{% \setevalue{Chosen#1}{\ctxlua{% local listsize = \letterhash #2 ; local LName = #2 ; tex.print(LName[math.random(1,listsize)])}}} \dorecurse{10}{\RandomChoice{Function}{ListOfNames}% Give an example of a function $\ChosenFunction : {\Bbb R} \longrightarrow {\Bbb R}$ which has a derivative only at the origin, and such that ${\ChosenFunction}'(0) = 1$.\par \hairline\par} \stoptext %%%% end choose-element.tex > On 18 Apr 2015, at 19:23, Wolfgang Schuster <schuster.wolfgang@gmail.com> wrote: > > >> Am 18.04.2015 um 18:55 schrieb Otared Kavian <otared@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 > > ___________________________________________________________________________________ > If your question is of interest to others as well, please add an entry to the Wiki! > > maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context > webpage : http://www.pragma-ade.nl / http://tex.aanhet.net > archive : http://foundry.supelec.fr/projects/contextrev/ > wiki : http://contextgarden.net > ___________________________________________________________________________________ ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: A macro which gives a random name 2015-04-18 20:24 ` Otared Kavian @ 2015-04-18 20:36 ` Hans Hagen 2015-04-19 7:51 ` Otared Kavian 0 siblings, 1 reply; 7+ messages in thread From: Hans Hagen @ 2015-04-18 20:36 UTC (permalink / raw) To: mailing list for ConTeXt users On 4/18/2015 10:24 PM, Otared Kavian wrote: > Hi again Wolfgang, > > Thanks to your hint, I could solve the problem… > In case someone else would encounter a similar problem to solve, below is a macro which chooses an element from a list, it creates a control sequence (CS) containing that element and it keeps the CS until the next time the macro is invoked again to choose another element. > > Thanks again and best regards: OK > > %%%% begin choose-element.tex > \setuprandomize[1989] % set a seed > > \starttext > > % here is a list from which a name is chosen > \startluacode > ListOfNames = {'F', 'G', 'u', 'v', 'W'} > \stopluacode > > % this macro has two arguments: > % the first argument is the control sequence name attached to Chosen, > % the second argument is the name of the list from which something is chosen > \define[2]\RandomChoice{% > \setevalue{Chosen#1}{\ctxlua{% > local listsize = \letterhash #2 ; > local LName = #2 ; > tex.print(LName[math.random(1,listsize)])}}} > > \dorecurse{10}{\RandomChoice{Function}{ListOfNames}% > Give an example of a function $\ChosenFunction : {\Bbb R} \longrightarrow {\Bbb R}$ which has a derivative only at the origin, and such that ${\ChosenFunction}'(0) = 1$.\par \hairline\par} > > \stoptext > %%%% end choose-element.tex \starttext \startluacode local FunctionNames = { "G" , "W" } local FunctionName = FunctionNames[1] function document.SetFunctionNames(list) FunctionNames = utilities.parsers.settings_to_array(list) end function document.GetFunctionName(new) if new then FunctionName = FunctionNames[math.random(1,#FunctionNames)] end context(FunctionName) end \stopluacode \def\SetFunctionNames[#1]{\ctxlua{document.SetFunctionNames("#1")}} \def\NewFunctionName {\ctxlua{document.GetFunctionName(true)}} \def\GetFunctionName {\ctxlua{document.GetFunctionName()}} \SetFunctionNames[a,b,c,d] \dorecurse{10}{ Give an example of a function $\NewFunctionName : {\Bbb R} \longrightarrow {\Bbb R}$ which has a derivative only at the origin, and such that $\GetFunctionName(0) = 1$. \par \hairline\par } \stoptext >> On 18 Apr 2015, at 19:23, Wolfgang Schuster <schuster.wolfgang@gmail.com> wrote: >> >> >>> Am 18.04.2015 um 18:55 schrieb Otared Kavian <otared@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 >> >> ___________________________________________________________________________________ >> If your question is of interest to others as well, please add an entry to the Wiki! >> >> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context >> webpage : http://www.pragma-ade.nl / http://tex.aanhet.net >> archive : http://foundry.supelec.fr/projects/contextrev/ >> wiki : http://contextgarden.net >> ___________________________________________________________________________________ > > ___________________________________________________________________________________ > If your question is of interest to others as well, please add an entry to the Wiki! > > maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context > webpage : http://www.pragma-ade.nl / http://tex.aanhet.net > archive : http://foundry.supelec.fr/projects/contextrev/ > wiki : http://contextgarden.net > ___________________________________________________________________________________ > -- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl ----------------------------------------------------------------- ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: A macro which gives a random name 2015-04-18 20:36 ` Hans Hagen @ 2015-04-19 7:51 ` Otared Kavian 2015-04-19 9:55 ` Hans Hagen 0 siblings, 1 reply; 7+ messages in thread From: Otared Kavian @ 2015-04-19 7:51 UTC (permalink / raw) To: mailing list for ConTeXt users Hi Hans, Many thanks for your wonderful code… I modified a little bit the names you use in your code so that it may be used for other purposes as well (for instance if one wishes to select at random a certain number of exercises from different subsets among a huge a dataset of problems). Again, for other people's possible needs, I put the modified code below (I don’t know in what category one might put your solution on the Wiki). If I may ask three questions in order to understand better your code, I would like to know 1) Why is it necessary to have this line (please see below) local ListOfNames = { "G" , "W » } in the luacode. Is it only a sort of intitialisation? 2) You use a built-in function utilities.parsers.settings_to_array(list) is it a ConTeXt function defined somewhere in the core, or a Lua function? 3) Assuming one has a list of names in a file names (say in a comma separated format) in a file named my-list.tex how is it possible to use it in \SetListOfFunctionNames? Using \SetListOfFunctionNames[\input named my-list.tex] results in an error since \directlua does not accept \input. Best regards: OK %%%% begin choose-random-names.tex by Hans \startluacode local ListOfNames = { "G" , "W" } local ChosenName = ListOfNames[1] function document.SetListOfNames(list) ListOfNames = utilities.parsers.settings_to_array(list) end function document.GetChosenName(new) if new then ChosenName = ListOfNames[math.random(1,#ListOfNames)] end context(ChosenName) end \stopluacode \def\SetListOfFunctionNames[#1]{\ctxlua{document.SetListOfNames("#1")}} \def\NewFunctionName {\ctxlua{document.GetChosenName(true)}} \def\FunctionName {\ctxlua{document.GetChosenName()}} \SetListOfFunctionNames[a,b,c,d] \dorecurse{10}{ Give an example of a function $\NewFunctionName : {\Bbb R} \longrightarrow {\Bbb R}$ which has a derivative only at the origin, and such that $\FunctionName(0) = 1$. \par \hairline\par } \stoptext %%%% end choose-random-names.tex by Hans > On 18 Apr 2015, at 22:36, Hans Hagen <pragma@wxs.nl> wrote: > > On 4/18/2015 10:24 PM, Otared Kavian wrote: >> Hi again Wolfgang, >> >> Thanks to your hint, I could solve the problem… >> In case someone else would encounter a similar problem to solve, below is a macro which chooses an element from a list, it creates a control sequence (CS) containing that element and it keeps the CS until the next time the macro is invoked again to choose another element. >> >> Thanks again and best regards: OK >> >> %%%% begin choose-element.tex >> \setuprandomize[1989] % set a seed >> >> \starttext >> >> % here is a list from which a name is chosen >> \startluacode >> ListOfNames = {'F', 'G', 'u', 'v', 'W'} >> \stopluacode >> >> % this macro has two arguments: >> % the first argument is the control sequence name attached to Chosen, >> % the second argument is the name of the list from which something is chosen >> \define[2]\RandomChoice{% >> \setevalue{Chosen#1}{\ctxlua{% >> local listsize = \letterhash #2 ; >> local LName = #2 ; >> tex.print(LName[math.random(1,listsize)])}}} >> >> \dorecurse{10}{\RandomChoice{Function}{ListOfNames}% >> Give an example of a function $\ChosenFunction : {\Bbb R} \longrightarrow {\Bbb R}$ which has a derivative only at the origin, and such that ${\ChosenFunction}'(0) = 1$.\par \hairline\par} >> >> \stoptext >> %%%% end choose-element.tex > > \starttext > > \startluacode > local FunctionNames = { "G" , "W" } > local FunctionName = FunctionNames[1] > > function document.SetFunctionNames(list) > FunctionNames = utilities.parsers.settings_to_array(list) > end > function document.GetFunctionName(new) > if new then > FunctionName = FunctionNames[math.random(1,#FunctionNames)] > end > context(FunctionName) > end > \stopluacode > > \def\SetFunctionNames[#1]{\ctxlua{document.SetFunctionNames("#1")}} > \def\NewFunctionName {\ctxlua{document.GetFunctionName(true)}} > \def\GetFunctionName {\ctxlua{document.GetFunctionName()}} > > \SetFunctionNames[a,b,c,d] > > \dorecurse{10}{ > Give an example of a function > $\NewFunctionName : {\Bbb R} \longrightarrow {\Bbb R}$ > which has a derivative only at the origin, and such that > $\GetFunctionName(0) = 1$. > \par \hairline\par > } > > > \stoptext > > > >>> On 18 Apr 2015, at 19:23, Wolfgang Schuster <schuster.wolfgang@gmail.com> wrote: >>> >>> >>>> Am 18.04.2015 um 18:55 schrieb Otared Kavian <otared@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 >>> >>> ___________________________________________________________________________________ >>> If your question is of interest to others as well, please add an entry to the Wiki! >>> >>> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context >>> webpage : http://www.pragma-ade.nl / http://tex.aanhet.net >>> archive : http://foundry.supelec.fr/projects/contextrev/ >>> wiki : http://contextgarden.net >>> ___________________________________________________________________________________ >> >> ___________________________________________________________________________________ >> If your question is of interest to others as well, please add an entry to the Wiki! >> >> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context >> webpage : http://www.pragma-ade.nl / http://tex.aanhet.net >> archive : http://foundry.supelec.fr/projects/contextrev/ >> wiki : http://contextgarden.net >> ___________________________________________________________________________________ >> > > > -- > > ----------------------------------------------------------------- > Hans Hagen | PRAGMA ADE > Ridderstraat 27 | 8061 GH Hasselt | The Netherlands > tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com > | www.pragma-pod.nl > ----------------------------------------------------------------- > ___________________________________________________________________________________ > If your question is of interest to others as well, please add an entry to the Wiki! > > maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context > webpage : http://www.pragma-ade.nl / http://tex.aanhet.net > archive : http://foundry.supelec.fr/projects/contextrev/ > wiki : http://contextgarden.net > ___________________________________________________________________________________ ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: A macro which gives a random name 2015-04-19 7:51 ` Otared Kavian @ 2015-04-19 9:55 ` Hans Hagen 0 siblings, 0 replies; 7+ messages in thread From: Hans Hagen @ 2015-04-19 9:55 UTC (permalink / raw) To: mailing list for ConTeXt users On 4/19/2015 9:51 AM, Otared Kavian wrote: > Hi Hans, > > Many thanks for your wonderful code… > I modified a little bit the names you use in your code so that it may be used for other purposes as well (for instance if one wishes to select at random a certain number of exercises from different subsets among a huge a dataset of problems). > Again, for other people's possible needs, I put the modified code below (I don’t know in what category one might put your solution on the Wiki). > > If I may ask three questions in order to understand better your code, I would like to know > > 1) Why is it necessary to have this line (please see below) > local ListOfNames = { "G" , "W » } > in the luacode. Is it only a sort of intitialisation? i just wanted a starting point (after all you put them in there) but you can start out local ListOfNames = { "unset" } > 2) You use a built-in function > utilities.parsers.settings_to_array(list) > is it a ConTeXt function defined somewhere in the core, or a Lua function? no, one of the context ones (see cld manual for more) ... lots of such helpers > 3) Assuming one has a list of names in a file names (say in a comma separated format) in a file > named my-list.tex > how is it possible to use it in \SetListOfFunctionNames? Using > \SetListOfFunctionNames[\input named my-list.tex] > results in an error since \directlua does not accept \input. something string.strip(io.loaddata(resolvers.findfile("foo.txt"))) or \cldloadfile{foo.txt} > Best regards: OK > %%%% begin choose-random-names.tex by Hans > \startluacode > local ListOfNames = { "G" , "W" } > local ChosenName = ListOfNames[1] > > function document.SetListOfNames(list) > ListOfNames = utilities.parsers.settings_to_array(list) > end > function document.GetChosenName(new) > if new then > ChosenName = ListOfNames[math.random(1,#ListOfNames)] > end > context(ChosenName) > end > \stopluacode > > \def\SetListOfFunctionNames[#1]{\ctxlua{document.SetListOfNames("#1")}} > \def\NewFunctionName {\ctxlua{document.GetChosenName(true)}} > \def\FunctionName {\ctxlua{document.GetChosenName()}} > > \SetListOfFunctionNames[a,b,c,d] > > \dorecurse{10}{ > Give an example of a function > $\NewFunctionName : {\Bbb R} \longrightarrow {\Bbb R}$ > which has a derivative only at the origin, and such that > $\FunctionName(0) = 1$. > \par \hairline\par > } > > \stoptext > %%%% end choose-random-names.tex by Hans > > >> On 18 Apr 2015, at 22:36, Hans Hagen <pragma@wxs.nl> wrote: >> >> On 4/18/2015 10:24 PM, Otared Kavian wrote: >>> Hi again Wolfgang, >>> >>> Thanks to your hint, I could solve the problem… >>> In case someone else would encounter a similar problem to solve, below is a macro which chooses an element from a list, it creates a control sequence (CS) containing that element and it keeps the CS until the next time the macro is invoked again to choose another element. >>> >>> Thanks again and best regards: OK >>> >>> %%%% begin choose-element.tex >>> \setuprandomize[1989] % set a seed >>> >>> \starttext >>> >>> % here is a list from which a name is chosen >>> \startluacode >>> ListOfNames = {'F', 'G', 'u', 'v', 'W'} >>> \stopluacode >>> >>> % this macro has two arguments: >>> % the first argument is the control sequence name attached to Chosen, >>> % the second argument is the name of the list from which something is chosen >>> \define[2]\RandomChoice{% >>> \setevalue{Chosen#1}{\ctxlua{% >>> local listsize = \letterhash #2 ; >>> local LName = #2 ; >>> tex.print(LName[math.random(1,listsize)])}}} >>> >>> \dorecurse{10}{\RandomChoice{Function}{ListOfNames}% >>> Give an example of a function $\ChosenFunction : {\Bbb R} \longrightarrow {\Bbb R}$ which has a derivative only at the origin, and such that ${\ChosenFunction}'(0) = 1$.\par \hairline\par} >>> >>> \stoptext >>> %%%% end choose-element.tex >> >> \starttext >> >> \startluacode >> local FunctionNames = { "G" , "W" } >> local FunctionName = FunctionNames[1] >> >> function document.SetFunctionNames(list) >> FunctionNames = utilities.parsers.settings_to_array(list) >> end >> function document.GetFunctionName(new) >> if new then >> FunctionName = FunctionNames[math.random(1,#FunctionNames)] >> end >> context(FunctionName) >> end >> \stopluacode >> >> \def\SetFunctionNames[#1]{\ctxlua{document.SetFunctionNames("#1")}} >> \def\NewFunctionName {\ctxlua{document.GetFunctionName(true)}} >> \def\GetFunctionName {\ctxlua{document.GetFunctionName()}} >> >> \SetFunctionNames[a,b,c,d] >> >> \dorecurse{10}{ >> Give an example of a function >> $\NewFunctionName : {\Bbb R} \longrightarrow {\Bbb R}$ >> which has a derivative only at the origin, and such that >> $\GetFunctionName(0) = 1$. >> \par \hairline\par >> } >> >> >> \stoptext >> >> >> >>>> On 18 Apr 2015, at 19:23, Wolfgang Schuster <schuster.wolfgang@gmail.com> wrote: >>>> >>>> >>>>> Am 18.04.2015 um 18:55 schrieb Otared Kavian <otared@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 >>>> >>>> ___________________________________________________________________________________ >>>> If your question is of interest to others as well, please add an entry to the Wiki! >>>> >>>> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context >>>> webpage : http://www.pragma-ade.nl / http://tex.aanhet.net >>>> archive : http://foundry.supelec.fr/projects/contextrev/ >>>> wiki : http://contextgarden.net >>>> ___________________________________________________________________________________ >>> >>> ___________________________________________________________________________________ >>> If your question is of interest to others as well, please add an entry to the Wiki! >>> >>> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context >>> webpage : http://www.pragma-ade.nl / http://tex.aanhet.net >>> archive : http://foundry.supelec.fr/projects/contextrev/ >>> wiki : http://contextgarden.net >>> ___________________________________________________________________________________ >>> >> >> >> -- >> >> ----------------------------------------------------------------- >> Hans Hagen | PRAGMA ADE >> Ridderstraat 27 | 8061 GH Hasselt | The Netherlands >> tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com >> | www.pragma-pod.nl >> ----------------------------------------------------------------- >> ___________________________________________________________________________________ >> If your question is of interest to others as well, please add an entry to the Wiki! >> >> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context >> webpage : http://www.pragma-ade.nl / http://tex.aanhet.net >> archive : http://foundry.supelec.fr/projects/contextrev/ >> wiki : http://contextgarden.net >> ___________________________________________________________________________________ > > ___________________________________________________________________________________ > If your question is of interest to others as well, please add an entry to the Wiki! > > maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context > webpage : http://www.pragma-ade.nl / http://tex.aanhet.net > archive : http://foundry.supelec.fr/projects/contextrev/ > wiki : http://contextgarden.net > ___________________________________________________________________________________ > -- ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl ----------------------------------------------------------------- ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : http://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________ ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2015-04-19 9:55 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2015-04-18 16:55 A macro which gives a random name Otared Kavian 2015-04-18 17:23 ` Wolfgang Schuster 2015-04-18 19:16 ` Otared Kavian 2015-04-18 20:24 ` Otared Kavian 2015-04-18 20:36 ` Hans Hagen 2015-04-19 7:51 ` Otared Kavian 2015-04-19 9:55 ` Hans Hagen
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).