4

Let's say I have two possible questions. I want only one to be on the generated PDF and I want this one to be randomly selected among the two potential candidates.

I thought this would be sufficient: (minimal code)

\documentclass{article} 
\usepackage{tikz}

\begin{document}

\pgfmathifthenelse{\pgfmathrnd >= 0.5}{%
My first question.%
Potentially has more than one line.%
}{%
My second question%
Potentially has more than one line%
}

\end{document}

This seems logical to me: I generate a random number between 0 and 1, if it's greater than 0.5 I have a version, if it's lower than 0.5 I have another version, and since the random number is generated at each compile I have one of the two versions at each compile.

However, I get loads of errors. I figured that maybe I can't break the {...} of the \ifthenelse across lines, so for the sake of understanding I tried

\ifthenelse{\pgfmathrnd >= 0.5}{A}{B}

But still to no avail. I don't really understand if it should be A of "A" but the latter doesn't work either.

I also tried using etoolbox but without better results. (And as far as I understand it, the toggle has to be declared in the preamble, and in the best of words my two possible questions would be in a separate file that I would just input into the main file.)


Background and why I want to do that

I am doing a pen-and-paper experiment (think of it as a quiz or a survey). Questions have two possible answers. I want the order of the answers to be random to prevent order effects. Hence I want the question to be randomly

This is the text of the question.

(1) Answer alpha

(2) Answer gamma

Please answer (1) or (2)

or

This is the text of the question.

(1) Answer gamma

(2) Answer alpha

Please answer (1) or (2)

Say my experiment is a file total.tex and my questions are in a questions.tex. If I have 10 subjects, I will loop 10 times on the \include{questions.tex} to generate 10 decision sheets. I want those 10 decision sheets to be randomly different, hence my question.

4
  • 1
    One thing to consider: the random number generator is deterministic so to get different sequences you need to alter the initial seed with \pgfmathsetseed{1234} (1234 being the seed). Every time you compile you'll get the same random sequence; the individual sheets in the same document however will be different from each other.
    – Bordaigorl
    Oct 17, 2014 at 9:44
  • To begin with, \ifthenelse doesn't know >= and doesn't accept decimal numbers.
    – egreg
    Oct 17, 2014 at 9:46
  • Also, \ifthenelse is not a LaTeX command, are you including other packages such as ifthen?
    – Bordaigorl
    Oct 17, 2014 at 9:57
  • Sorry, I made a mistake. I meant \pgfmathifthenelse not simply \ifthenelse.
    – darpich
    Oct 17, 2014 at 12:07

2 Answers 2

3

If you want to stick with PGF's random function

\documentclass{article}
\usepackage{pgffor}  % you do not need full tikz for this

\begin{document}

\foreach \i in {1,...,10}{
  \pgfmathrandom{2}  % store 1 or 2 in \pgfmathresult
  \ifnum\pgfmathresult>1%
    My first question.%
    Potentially has more than one line.%
  \else
    My second question%
    Potentially has more than one line%
  \fi
  \par
}

\end{document}

Keep in mind that every time you compile PGF will generate the same sequence of random numbers unless you specify a different seed using \pgfmathsetseed. To each different seed will correspond a different random sequence.

1
  • A late update, but I finally used this one. The other answer was finally too cumbersome to implement (I wanted to use \emph and I had to use \noexpand all the time)
    – darpich
    Nov 18, 2014 at 16:56
4

The macro \ifthenelse is not provided by default, but needs \usepackage{ifthen} or, better, \usepackage{xifthen}. However, its tests don't (easily) allow for decimal number comparisons.

You can do it with PGF features:

\documentclass{article}
\usepackage{tikz}

\newcommand{\first}{My first question.
Potentially has more than one line.}
\newcommand{\second}{My second question.
Potentially has more than one line.}

\begin{document}

\pgfmathparse{rnd >= 0.5 ? "\first" : "\second"}\pgfmathresult

\pgfmathparse{rnd >= 0.5 ? "\first" : "\second"}\pgfmathresult

\pgfmathparse{rnd >= 0.5 ? "\first" : "\second"}\pgfmathresult

\end{document}

Of course you would define a macro for that, but this is left as an exercise.

enter image description here

1
  • the x ? y : z thing is just amazing. I had read again and again the manual but I didn't think of checking under 'Basic operators'. That's just so powerful!
    – darpich
    Oct 17, 2014 at 12:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .