# creating a list of random numbers using a loop

I am trying to write a loop that will generate a set of random variables to be used in the probsoln package.

I would like to make a loop to generate A, B, C, etc. This exam will have a lot of questions and I would prefer to not define each individually. Then I would like to pass those numbers into the selected problem. I have tried using the lcg package, but I think that there might be some compatibility issues with the probsoln package.

Here is the working code (without the loop):

\documentclass[12pt,a4paper,twocolumn]{article}
\usepackage[pdftex]{graphicx}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{amsfonts} %This contains extra fonts needed for math
\usepackage{amscd}  %This allows you to draw commutative diagrams.
\usepackage{probsoln}
\usepackage{multicol}
\usepackage{enumerate}
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes,shapes.multipart}
\usetikzlibrary{arrows,backgrounds,snakes}
\usepackage{fancyhdr}

\usepackage[
top    = 1.50cm,
bottom = 1.50cm,
left   = 1.50cm,
right  = 1.50cm]{geometry}

\newcommand{\bigbrk}{\vspace*{2in}}
\newcommand{\bigbigbrk}{\vspace*{11in}}
\newcommand{\smallbrk}{\vspace*{.3in}}
\setcounter{section}{0}
\newcommand*\mycirc[1]{%
\begin{tikzpicture}
\node[draw,circle,inner sep=1pt] {#1};
\end{tikzpicture}}

\input{ac-ma}

\newcounter{A}
\newcounter{B}
\newcounter{C}
\random{A}{1}{9}
\random{B}{1}{9}
\random{C}{1}{9}

\begin{document}

\begin{enumerate}
\item  \useproblem{AC.MA:DIST}{\arabic{A}}{\arabic{B}}
\end{enumerate}

\end{document}
\endinput


This .tex file is pulling from a problems database, which is in the code below:

### ac-ma.tex

\def\add#1#2{
\newcount\tmp
\newcount\tmptwo
\tmp=#1
\tmptwo=1
\number\tmp
}

\begin{defproblem}[2]{AC.MA:DIST}
If $(#1.95+#2.05)x=x$, then what is the value of $x$?
\begin{multicols}{4}
\begin{enumerate}
\correctitem $\displaystyle{\frac{1}{\add{#1}{#2}}}$
\incorrectitem $\add{#1}{#2}$
\incorrectitem $\add{-#1}{#2}$
\incorrectitem $\add{#1}{-#2}$
\end{enumerate}
\end{multicols}
\end{defproblem}

\begin{defproblem}[1]{GE.LA:E2}
$\angle A$ is a supplement of $\angle B$.  If the measure of $\angle A$ is $#1^\circ$, what is the measure of $\angle B$?
\begin{multicols}{4}
\begin{enumerate}
\end{enumerate}
\end{multicols}
\end{defproblem}

\begin{defproblem}[1]{GE.LA:E3}
$\angle P$ is a supplement of $\angle Q$.  If the measure of $\angle P$ is $(#1x)^\circ$, what is the measure of $\angle Q$?
\begin{multicols}{4}
\begin{enumerate}
\incorrectitem $90-#1x$
\incorrectitem $90+#1x$
\incorrectitem $180-#1x$
\incorrectitem $180+#1x$
\end{enumerate}
\end{multicols}
\end{defproblem}

\endinput
%%
%% End of file ac-ma.tex'.


Any input or advice would be greatly appreciated! I am sorry for all of the packages in the preamble, but I will need them for later questions.

Thank you for the help, but now I am trying to pass the numbers into the select randomly function by adding a line under enumerate:

\item  \selectrandomly{ac-ma}{1}{\myrandomC}


Unfortunately, it doesn't want to accept my arguments and I keep getting this error:

Problem AC.MA:SYS' (in data base ac-ma') requires 1 argument(s).


Do I need to wrap the inputs in something? I have tried using \thisproblem and \foreachproblem, but I keep getting the same error.

@grames, I am now using the \esami package and I am trying to pull a random set of questions from the test1 file. Looking through the documentation, it looks like the appropriate command is \estraies[3]{test1}{beta}. I would then pass the questions: beta1, beta2, etc. into the \esercizi{beta1,beta2,beta3} command line. Unfortunately, it looks like I am doing something wrong because I keep getting the following error:

! LaTeX Error: Lonely \item--perhaps a missing list environment.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.40 }

?


Here is the code:

test1.tex

\newproblem{prob1
\FPsetpar{a}{0}{10}
\FPsetpar{b}{0}{10}
\item  If $(\a.95+\b.05)x=x$, then what is the value of $x$?
\vspace{10 mm}
\Ans1 $0$ \eAns
\Ans0 \FPsv{a+b+1}\eAns
\Ans0 \FPsv{a+b}\eAns
\Ans0 $\displaystyle{\frac{1}{\FPsv{a+b}}}$ \eAns

\newproblem{prob2
\FPsetpar{a}{2}{5}
\item  For what value of $x$ is the equation \begin{equation*}  \frac{x-\a}{\a+x}=0 \end{equation*} true?
\vspace{10 mm}
\Ans0 -\a \eAns
\Ans1 \a \eAns  \eFreeze
\Ans0 all real numbers \eAns  \eFreeze
\Ans0 There are no values for $x$ that would make the equation true. \eAns

\newproblem{prob3
\FPsetpar{a}{2}{5}
\item  If $\a x-\a=xy-y$ and $x\neq1$, what is the value of $y$?
\vspace{10 mm}
\Ans1 \a \eAns
\Ans0 -\a \eAns
\Ans0 0 \eAns \eFreeze
\Ans0 There are no values for $x$ that would make the equation true. \eAns

\newproblem{prob4
\FPsetpar{a}{2}{5}
\item  If $\a x-\a=xy-y$ and $x\neq1$, what is the value of $y$?
\vspace{10 mm}
\Ans1 \a \eAns
\Ans0 -\a \eAns
\Ans0 0 \eAns \eFreeze
\Ans0 There are no values for $x$ that would make the equation true. \eAns

master_new.tex

\documentclass[a4paper,UKenglish]{article}
\usepackage[shufflerandom,xyz,twocolumns, autopstoff,nosolutions]{esami}
\usepackage{lmodern,textcomp}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

% +=============================+
% |         DEFINITIONS         |
% +=============================+

\def\examname{The name of the exam}
\def\numcompiti{1}%%% The number of the versions
\date{2013/3/22}%%% THE DATE IN THE FORMAT YYYY/MM/DD
\def\Data{\longdate}%%% or \shortdate: the date in the heading

\begin{document}
\testversioni
\pagestyle{esame}
\whiledo{\thevers<\numcompiti}{\stepvers

% +=============================+
% |       THE SEED              |
% +=============================+

\FPeval\seme{round((\thenomefile+(3*\thevers)):0)}

\randomi=\seme
\permuta

\estraies[0]{test1}{beta}

\section*{Mathematics Achievement}
\begin{test}
\begin{questions}
\esercizi{betai,betaii}
\end{questions}
\end{test}

%\closevers
}
\end{document}


Any help would be greatly appreciated. Thanks MeTutel

@grames ok, that's good to know. I haven't been able to get that working yet, but I will keep playing around with it. I have another question. Your package is using the PSTricks package for graphics, which from the documentation should be in the workarea space. I have changed the option to autostopon, but the graphic I am trying to generate is still not working. I hope that all of the questions are not a nuisance, but I am trying to read through the documentation and examples and play with the code before I submit questions. Here is the code I am using, problem file first. eachtest.tex

\newproblem{
\item  Determine the are of the figure below.

\begin{workarea}[10mm]{10mm}
\begin{pspicture}(4,5)
\psframe(0.7,2)(3.3,3)
\rput(2,2.5){First Example}
\end{pspicture}
\end{workarea}

\vspace{10 mm}
\Ans0 Wrong \eAns
\Ans1 Right \eAns
\Ans0 Wrong \eAns
\Ans0 Wrong \eAns
}'

\documentclass[a4paper,UKenglish]{article}
\usepackage[shufflerandom,autopston,xyz,twocolumns,solutions]{esami}
\usepackage{lmodern,textcomp}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

% +=============================+
% |         DEFINITIONS         |
% +=============================+

\def\examname{The name of the exam}
\def\numcompiti{1}%%% The number of the versions
\date{2013/3/22}%%% THE DATE IN THE FORMAT YYYY/MM/DD
\def\Data{\longdate}%%% or \shortdate: the date in the heading

\begin{document}
\testversioni

\pagestyle{esame}
\whiledo{\thevers<\numcompiti}{\stepvers

% +=============================+
% |       THE SEED              |
% +=============================+

\FPeval\seme{round((\thenomefile+(3*\thevers)):0)}

\randomi=\seme
\permuta

\section*{Mathematics Achievement}
\begin{test}
\begin{questions}
\esercizi{eachtest}
%\esercizi{probs-ac-ma,probs-ge-ma,probs-ge-qr}
\end{questions}
\end{test}

\closevers
}
\end{document}

-
See Martin Scharrer's list of first 500 random numbers, listed in order for convenience: tex.stackexchange.com/a/18329/1410 :-D –  morbusg Apr 16 '13 at 6:57
Thank you so so so much for this! It will save me a ton of time. Yes, the answer is wrong...I kinda posted this in the middle of writing a set because I didn't think I would get an answer so quickly. –  user1932356 Apr 16 '13 at 18:37
At the moment you cannot extract more than one variant from a file using esami.sty, as you can do in probsoln. We are working on it. For now, you can do this: 1) write each problem (with its variants) in a different file, test1, test2, ecc. (the string prob1, ecc. at the beginning must be eliminated) 2) use the command \esercizi{} to choose a variant from each file in braces, or the command \estraies to choose n-m files from the n in braces, or a combination of \estrai and \esercizi which gives the same result. \estraies and \esercizi should not be used together. –  grames Apr 20 '13 at 21:52
ok, that's good to know. I haven't been able to get that working yet, but I will keep playing around with it. I have another question. Your package is using the PSTricks package for graphics, which from the documentation should be in the workarea space. I have changed the option to autostopon, but the graphic I am trying to generate is still not working. –  MeTutel Apr 21 '13 at 12:05
The workarea space is an environment which can contain graphics, but it works only inside a problem environment and the stuffs you place in it appear for the student with the nosolutions option and are replaced by the solution when you choose the solutions option. In your case, you are using a test environment, so you should remove the workarea environment in which you have placed the pstricks code and everything should work. The correct option for pstricks is autopston. Please note that you must call pdflatex with shell-escape (Texlive) or write18 (Miktex) option for this option to work. –  grames Apr 21 '13 at 15:09

Since you are already using TikZ, you can use the random() function of pgfmath module; random() gives a random real number in [0,1], random(x) gives an integer in [1,x] and random(x,y) gives an integer in [x,y]. Then the resulting random integers (well replace all random words with pseudo-random) are saved to macros \myrandomA,\myrandomB...

I have also removed/modified some package declarations since some load the others etc. and used filecontents package to create ac-ma.tex on the fly.

\PassOptionsToPackage{usenames,dvipsnames,svgnames,table}{xcolor}
\documentclass[12pt,a4paper,twocolumn]{article}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{amsfonts} %This contains extra fonts needed for math
\usepackage{amscd}  %This allows you to draw commutative diagrams.
\usepackage{probsoln}
\usepackage{multicol}
\usepackage{enumerate}
\usepackage{filecontents}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{shapes,shapes.multipart}
\usetikzlibrary{arrows,backgrounds,snakes}
\usepackage{fancyhdr}
\usepackage[
top    = 1.50cm,
bottom = 1.50cm,
left   = 1.50cm,
right  = 1.50cm]{geometry}

\begin{filecontents}{ac-ma.tex}
\newcount\tmp
\newcount\tmptwo
\tmp=#1
\tmptwo=1
\number\tmp
}

\begin{defproblem}[2]{AC.MA:DIST}
If $(#1.95+#2.05)x=x$, then what is the value of $x$?
\begin{multicols}{4}
\begin{enumerate}
\correctitem $\displaystyle{\frac{1}{\add{#1}{#2}}}$
\incorrectitem $\add{#1}{#2}$
\incorrectitem $\add{-#1}{#2}$
\incorrectitem $\add{#1}{-#2}$
\end{enumerate}
\end{multicols}
\end{defproblem}

\begin{defproblem}[1]{GE.LA:E2}
$\angle A$ is a supplement of $\angle B$.  If the measure of $\angle A$ is $#1^\circ$, what is the measure of $\angle B$?
\begin{multicols}{4}
\begin{enumerate}
\end{enumerate}
\end{multicols}
\end{defproblem}

\begin{defproblem}[1]{GE.LA:E3}
$\angle P$ is a supplement of $\angle Q$.  If the measure of $\angle P$ is $(#1x)^\circ$, what is the measure of $\angle Q$?
\begin{multicols}{4}
\begin{enumerate}
\incorrectitem $90-#1x$
\incorrectitem $90+#1x$
\incorrectitem $180-#1x$
\incorrectitem $180+#1x$
\end{enumerate}
\end{multicols}
\end{defproblem}

\endinput
%%
%% End of file ac-ma.tex'.
\end{filecontents}

\newcommand{\bigbrk}{\vspace*{2in}}
\newcommand{\bigbigbrk}{\vspace*{11in}}
\newcommand{\smallbrk}{\vspace*{.3in}}
\setcounter{section}{0}
\newcommand*\mycirc[1]{%
\begin{tikzpicture}
\node[draw,circle,inner sep=1pt] {#1};
\end{tikzpicture}}

\input{ac-ma}

%================================ MODIFIED
%================================
\foreach \x in {A,...,C}{
\pgfmathparse{random(9)}
\expandafter\xdef\csname myrandom\x\endcsname{\pgfmathresult}
}

\begin{document}

\begin{enumerate}
\item  \useproblem{AC.MA:DIST}{\myrandomA}{\myrandomB}
\end{enumerate}
%================================
%================================
\end{document}


However the question seems to be wrong since the answer seems to be zero regardless.

-
I have one more question: if I try to pass the variables into the select randomly function, I get an error that I am not specifying the arguments: I just tried adding another line under enumerate:\item \selectrandomly{ac-ma}{1}{\myrandomC}{\myrandomD} –  MeTutel Apr 17 '13 at 13:54
@MeTutel I've only defined up to C so you need to change A,...,H or whatever letter you need random variables to be defined. –  percusse Apr 17 '13 at 13:57
yes, but even after making that change it still will not accept the argument. the manual says that I should just specify the arguments following the function in brackets, but I'm not sure what I'm missing. To be 100% sure it wasn't the random numbers, I even tried hard-coding an integer. still no luck.\foreach \x in {A,...,Z}{ \pgfmathparse{random(9)} \expandafter\xdef\csname myrandom\x\endcsname{\pgfmathresult} } \begin{document} \begin{enumerate} %\item \useproblem{AC.MA:DIST}{\myrandomC}{\myrandomD} \item \selectrandomly{ac-ma_1arg}{1}{\myrandomC} \end{enumerate} –  MeTutel Apr 17 '13 at 14:47
@MeTutel Some macros expect a number instead of a macro hence the numbers probably need to be expanded. Try \item \begingroup\edef\temp{\endgroup\noexpand\selectrandomly{ac-ma_1arg}{1}{\myrandom‌​C}}\temp instead of the problematic line. I can't because I don't have that file –  percusse Apr 17 '13 at 14:57
I switched to the esami package - they have a lot of this stuff already built in. Thank you so much for your help! –  MeTutel Apr 18 '13 at 12:56