I'm trying to define random variables – for example to draw some random points of the form (r,r) when r is a random number. I tried \def\myrandomnumber{rnd}
, but that seems to give me a new random number every time. What's to be done?
[Tex/LaTex] Defining variables in TikZ
random numberstikz-pgf
Related Solutions
The problem is that you're doing that inside a table cell and TeX doesn't like it very much. It's better to build up the token list before doing the table:
\documentclass{article}
\usepackage{lcg,forloop}
\newtoks\dierckxtoks
\newcounter{row_number}\newcounter{col_number}
\begin{document}
\chgrand[first=0, last=4, counter=kids]
\dierckxtoks={}
\forloop{row_number}{1}{\value{row_number} < 6}{%
\forloop{col_number}{1}{\value{col_number} < 5}{%
\rand
\edef\x{\the\dierckxtoks\arabic{kids} &}
\dierckxtoks\expandafter{\x}%
}%
\rand
\edef\x{\the\dierckxtoks\arabic{kids} \noexpand\\}
\dierckxtoks\expandafter{\x}%
}
\begin{tabular}{rrrrr}
\the\dierckxtoks
\end{tabular}
\end{document}
On the other hand, as shown in my first comment, also 444444444 can be a sequence of random numbers. :-)
The mandatory expl3
solution.
\documentclass{article}
\usepackage{lcg}
\newcounter{randnumb}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\randomtabular}{ O{randnumb} m m m m }
% #1 = counter name (optional default randnumb)
% #2 = lowest value
% #3 = highest value
% #4 = rows
% #5 = columns
{
\chgrand[first=#2, last=#3, counter=#1]
\dierckx_random_tabular:nnn {#1}{#4}{#5}
}
\tl_new:N \l_dierckx_tabular_tl
\cs_new_protected:Npn \dierckx_random_tabular:nnn #1 #2 #3
{
\tl_clear:N \l_dierckx_tabular_tl
\prg_replicate:nn { #2 }
{
\prg_replicate:nn { #3 - 1 }
{
\rand
\tl_put_right:Nx \l_dierckx_tabular_tl { \arabic{#1} & }
}
\rand
\tl_put_right:Nx \l_dierckx_tabular_tl { \arabic{#1} }
\tl_put_right:Nn \l_dierckx_tabular_tl { \\ }
}
\begin{tabular}{*{#3}{r}}
\l_dierckx_tabular_tl
\end{tabular}
}
\ExplSyntaxOff
\begin{document}
\randomtabular{0}{9}{6}{4}
\randomtabular[kids]{0}{4}{5}{5}
\end{document}
A different solution using the random number facility of pgf
:
\documentclass{article}
\usepackage{pgf}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\randomtabular}{ m m m m }
% #1 = lowest value
% #2 = highest value
% #3 = rows
% #4 = columns
{
\dierckx_random_tabular:nnnn {#1}{#2}{#3}{#4}
}
\tl_new:N \l__dierckx_tabular_tl
\int_new:N \l__dierckx_random_number_int
\cs_new_protected:Npn \dierckx_random_tabular:nnnn #1 #2 #3 #4
{
\tl_clear:N \l_dierckx_tabular_tl
\prg_replicate:nn { #3 }
{
\prg_replicate:nn { #4 - 1 }
{
\dierckx_get_rand:nn { #1 } { #2 }
\tl_put_right:Nx \l_dierckx_tabular_tl { \int_to_arabic:n { \l__dierckx_random_number_int } & }
}
\dierckx_get_rand:nn { #1 } { #2 }
\tl_put_right:Nx \l_dierckx_tabular_tl { \int_to_arabic:n { \l__dierckx_random_number_int } }
\tl_put_right:Nn \l_dierckx_tabular_tl { \\ }
}
\begin{tabular}{*{#3}{r}}
\l_dierckx_tabular_tl
\end{tabular}
}
\cs_new_protected:Npn \dierckx_get_rand:nn #1 #2
{
\pgfmathrandominteger{ \l__dierckx_random_number_int } { #1 } { #2 }
}
\ExplSyntaxOff
\begin{document}
\randomtabular{0}{9}{6}{4}
\bigskip
\randomtabular{0}{4}{5}{5}
\end{document}
There is no optional argument any more, but it doesn't seem to be really necessary.
A variant of the first expl3
solution that pads the number with zeros to have the same length as the highest possible chosen number.
\documentclass{article}
\usepackage{lcg}
\newcounter{randnumb}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\randomtabular}{ O{randnumb} m m m m }
% #1 = counter name (optional default randnumb)
% #2 = lowest value
% #3 = highest value
% #4 = rows
% #5 = columns
{
\int_set:Nn \l_dierckx_padto_int { \tl_count:n { #3 } }
\chgrand[first=#2, last=#3, counter=#1]
\dierckx_random_tabular:nnn {#1}{#4}{#5}
}
\tl_new:N \l_dierckx_tabular_tl
\tl_new:N \l_dierckx_temp_tl
\int_new:N \l_dierckx_padto_int
\cs_new_protected:Npn \__dierckx_padnumber:nn #1 #2
{
\tl_set:Nx \l_dierckx_temp_tl { \arabic{#1} }
\tl_set:Nx \l_dierckx_temp_tl
{
\prg_replicate:nn { \l_dierckx_padto_int - \tl_count:N \l_dierckx_temp_tl } { 0 }
\l_dierckx_temp_tl
}
\tl_put_right:Nx \l_dierckx_tabular_tl { \l_dierckx_temp_tl #2 }
}
\cs_new_protected:Npn \dierckx_random_tabular:nnn #1 #2 #3
{
\tl_clear:N \l_dierckx_tabular_tl
\prg_replicate:nn { #2 }
{
\prg_replicate:nn { #3 - 1 }
{
\rand
\__dierckx_padnumber:nn { #1 } { & }
}
\rand
\__dierckx_padnumber:nn { #1 } { }
\tl_put_right:Nn \l_dierckx_tabular_tl { \\ }
}
\begin{tabular}{*{#3}{r}}
\l_dierckx_tabular_tl
\end{tabular}
}
\ExplSyntaxOff
\begin{document}
\randomtabular{0}{100000}{6}{4}
\end{document}
Based on your question you could do this:
Code
\documentclass{article}
\usepackage{pgf}
\pgfmathsetseed{\number\pdfrandomseed} % provide seed for pseudo random generator
% (change to constant, to get the same random numbers on every time compiling)
% new command to init variables
\newcommand\initVariables{%
\pgfmathsetmacro{\A}{random(1,10)}%
\pgfmathsetmacro{\B}{random(10,20)}%
}
\newcommand\answer{\pgfmathprint{int(\A + \B)}} % define command to calculate result
\begin{document}
\initVariables % initialize variables (do this every time you need new numbers)
What is $\A + \B$?
Result is \answer
\end{document}
Result
What is 2 + 12?
Result is 14
Fun with layers ;)
You could use layers to hide the answer, until the solution layer is enabled (you didn't ask for it, so this is just for fun).
\documentclass{article}
\usepackage{pgf}
\usepackage{ocg-p}
\pgfmathsetseed{\number\pdfrandomseed} % provide seed for pseudo random generator
% new command to init variables
\newcommand\initVariables{%
\pgfmathsetmacro{\A}{random(1,10)}%
\pgfmathsetmacro{\B}{random(10,20)}%
}
\newcommand\answer{\pgfmathprint{int(\A + \B)}} %define command to calculate result
\begin{document}
\initVariables%initialize variables (do this every time you need new numbers)
What is $\A + \B$?
Result is
\begin{ocg}{result layer}{1}{0}
\answer
\end{ocg}
\end{document}
Best Answer
Here is an example of using
\pgfmathsetseed{}
to ensure that subsequent runs yield identical results with a random number. I also make use of Jake's solution using\pgfmathsetmacro
to store the result so that it can be used more than once.Depending on when you run this, the blue pictures will vary (since
\pgfmathsetseed{}
was not used), but the red ones should not change. Since each of the red images are identical it is obvious that the random numbers did not change between runs, but note that each of the three blue pictures are different.