[Tex/LaTex] creating a list of random numbers using a loop

loopsprobsolnrandom numbers

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}}

\loadrandomproblems[ac-ma]{1}{ac-ma}
\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
 \advance\tmp by #2
 \advance\tmp by \tmptwo
 \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}
\incorrectitem \add{#1}{90}
\incorrectitem \add{#1}{180}
\incorrectitem \add{-#1}{90}
\correctitem \add{-#1}{180}
\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).
 Please specify (e.g. {12}):

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}
      \begin{answers}{2}\bChoices[random]
        \Ans1 $0$ \eAns
        \Ans0 \FPsv{a+b+1}\eAns
        \Ans0 \FPsv{a+b}\eAns
        \Ans0 $\displaystyle{\frac{1}{\FPsv{a+b}}}$ \eAns
        \eChoices\end{answers}}
    
    \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}
      \begin{answers}{2}\bChoices
        \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
        \eChoices\end{answers}}
    
    \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}
      \begin{answers}{2}\bChoices[random]
        \Ans1 \a \eAns
        \Ans0 -\a \eAns
        \Ans0 0 \eAns \eFreeze
        \Ans0 There are no values for $x$ that would make the equation true. \eAns
        \eChoices\end{answers}}
    
    \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}
      \begin{answers}{2}\bChoices[random]
        \Ans1 \a \eAns
        \Ans0 -\a \eAns
        \Ans0 0 \eAns \eFreeze
        \Ans0 There are no values for $x$ that would make the equation true. \eAns
        \eChoices\end{answers}}


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}
  \begin{answers}{2}\bChoices[random]
    \Ans0 Wrong \eAns
    \Ans1 Right \eAns
    \Ans0 Wrong \eAns
    \Ans0 Wrong \eAns
    \eChoices\end{answers}
}'

\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}

Best Answer

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}
    \def\add#1#2{
 \newcount\tmp
 \newcount\tmptwo
 \tmp=#1
 \tmptwo=1
 \advance\tmp by #2
 \advance\tmp by \tmptwo
 \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}
\incorrectitem \add{#1}{90}
\incorrectitem \add{#1}{180}
\incorrectitem \add{-#1}{90}
\correctitem \add{-#1}{180}
\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}}

\loadrandomproblems[ac-ma]{1}{ac-ma}
\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.

enter image description here

Related Question