From: "Meer, H. van der" <H.vanderMeer@uva.nl>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: How to define a command...
Date: Thu, 5 Jan 2012 20:11:21 +0000 [thread overview]
Message-ID: <DFE3FFC7-129D-4A28-A55E-640AECC5D215@uva.nl> (raw)
In-Reply-To: <6457E519-6F30-482B-98DC-08BD546F8B2F@gmail.com>
Reccently I did something random in ctxlua. I post it in the hope it will be useful.
Hans van der Meer
\startluacode
-- Define our namespace as hvdm
hvdm = hvdm or {}
-- Return random series of numbers 1..n depending on the number of arguments
function hvdm.randomseries (n, straight)
local done = {}
for i = 1, n do
done[i] = i
end
if straight == "false" then
return table.concat(done,",")
end
local result = ""
for i = n, 2, -1 do
local r = math.random(i)
result = result .. done[r] .. ","
table.remove(done,r)
end
result = result .. done[1]
return result
end
\stopluacode
\def\RandomSeed#1{\directlua{math.randomseed(#1)}}
\def\RandomValue{\directlua{tex.print(math.random())}}
\def\RandomRange#1{\directlua{tex.print(math.random(#1))}}
\def\RandomSeries[#1]#2{\ctxlua{tex.print(hvdm.randomseries(#2,"#1"))}%
On 5 jan. 2012, at 21:05, Otared Kavian wrote:
> Hi all,
>
> I need to define numerical coefficients (essentially at random) and then use them with commands, say like \CoeffAlpha, \CoeffBeta, and so on.
> More precisely, how can I define the command \RandomCoeff, so that \RandomCoeff{Beta}{1}{10} yields a command named \CoeffBeta, which is a random number chosen between 1 and 10?
>
> I tried to use the following approach, but could not make it work:
>
> %%% begin random-coeff.tex
> \setuprandomize[2012] % set a seed
>
> \ctxlua{CoeffAlpha = math.random(1,10) ;}
> \def\CoeffAlpha{\ctxlua{tex.print(CoeffAlpha)}}
>
> %%%%%
> \define[3]\RandomCoeff{%
> \ctxlua{a = math.random(#2,#3)}
> % \csname{Coeff#1}\endcsname{\ctxlua{tex.print(a)}} %% this line does not work as expected...
> }
>
> \starttext
> \CoeffAlpha
>
> \RandomCoeff{Beta}{1}{10}
>
> \ctxlua{tex.print(a)}
>
> %\CoeffBeta
>
> \stoptext
> %%% end random-coeff.tex
>
> Thanks in advance for any suggestion or hint.
> Best regards: OK
>
> ___________________________________________________________________________________
> 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
___________________________________________________________________________________
next prev parent reply other threads:[~2012-01-05 20:11 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-01-05 20:05 Otared Kavian
2012-01-05 20:11 ` Meer, H. van der [this message]
2012-01-05 20:21 ` Wolfgang Schuster
2012-01-05 20:28 ` Otared Kavian
2012-01-05 20:17 ` Peter Münster
2012-01-05 20:18 ` Wolfgang Schuster
2012-01-05 20:42 ` Otared Kavian
2012-01-05 21:24 ` Otared Kavian
2012-01-05 21:36 ` Otared Kavian
2012-01-05 20:29 ` Otared Kavian
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DFE3FFC7-129D-4A28-A55E-640AECC5D215@uva.nl \
--to=h.vandermeer@uva.nl \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).