How do I generate in LaTeX a list of random integers, in a given range, avoiding repetitions? The command \pgfmathrandomitem
selects an item from a list, but when inserted in a loop can select twice the same item. Since I am selecting questions from a list in order to prepare an exam, I cannot ask twice the same question.
[Tex/LaTex] Generating random numbers without repetitions
random numbers
Related Solutions
Following Ben Bolker's comment:
Simply copy and paste the entire question
environment. Edit the chunks' names. Sweave will then run the code twice and output the same text but with different numbers put in by \Sexpr
.
\begin{shortanswer}[title={\Large Short Answer (10 pts each)}]
\begin{question}
<<ifst1, echo=FALSE>>==
N = round(runif(1, 5, 20))
@
John wants to know whether his students are smarter than the average student.
John has \Sexpr{N} students \dots
\examvspace*{1.5in}
\begin{answer}
\end{answer}
\end{question}
\begin{question}
<<ifst2, echo=FALSE>>==
N = round(runif(1, 5, 20))
@
John wants to know whether his students are smarter than the average student.
John has \Sexpr{N} students \dots
\examvspace*{1.5in}
\begin{answer}
\end{answer}
\end{question}
\end{shortanswer}
This works, but heavily violates the DRY principle. With knitr, a successor of Sweave, you could write R code that creates the entire question
environment and writes it verbatim into the generated LaTeX code. (Perhaps this is possible with Sweave, too?) Something along these lines:
\begin{shortanswer}[...]
<<generate, echo=FALSE, format=asis>>=
for (i in 1:2) {
N = round(runif(1, 5, 20))
paste('\\begin{question}', 'John has', N, 'students', ..., '\\end{question}')
}
@
\end{shortanswer}
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.
Best Answer
You can remove the item from the list after selecting it, then it won't be picked again:
to declare an integer list: