1

I'm designing a questionnaire. I would like to do two things in particular in this design. I'm asking about both things in one question since I believe they are related:

First, I would like to have a random order for the items of the questionnaire. This part, I naively called \randomize in my WME (it's not actually working).

Second, I would like to call different names from my pool of names for the entire questionnaire. I don't know where to begin putting this into code.

Any randomising code I have looked at so far doesn't seems to be doing what I need. One of the more prominent reasons is that I would like to have maximum flexibility and be able to add sentences (and names) as I please.

Any help will be much appreciated – thanks a lot in advance.

\documentclass{article}

\newcommand{\NAMEi}{Sally}
\newcommand{\NAMEii}{Harry}
\newcommand{\NAMEiii}{Anne}
\newcommand{\NAMEiv}{Rob}

\newcommand{\SENTi}{When did \NAMEii \ meet \NAMEi ?}
\newcommand{\SENTii}{What did \NAMEiii \ call \NAMEi ?}
\newcommand{\SENTiii}{Does \NAMEiv \ like haning out with \NAMEi ?}

\begin{document}

\randomize{
\SENTi,\SENTii,\SENTiii
}

\end{document}
  • You can't combine numbers and letters in a macro unless you use \csname ...\endcsname. See tex.stackexchange.com/questions/346260/… for example. – John Kormylo May 18 '18 at 14:35
  • @JohnKormylo But those are roman numbers which are made out of letters?! – Skillmon likes topanswers.xyz May 18 '18 at 15:07
  • Ah, so they are. One can still use \@roman to convert. – John Kormylo May 18 '18 at 15:51
  • @Johnkormylo - Yes, those are roman number, i.e. letters, which works just fine and I did that exactly for the reason that I cannot use numbers in macros. – Mat May 21 '18 at 8:42
2

According to the manual, the random seed (see \pgfmathsetseed) defaults to time*year (although \time*\year is too large for \pgfmathparse). That means that the seed will change at most once every minute.

\documentclass{article}
\usepackage{pgfmath}

\newcommand{\NAMEi}{Sally}
\newcommand{\NAMEii}{Harry}
\newcommand{\NAMEiii}{Anne}
\newcommand{\NAMEiv}{Rob}

\newcommand{\SENTi}{When did \NAMEii \ meet \NAMEi ?}
\newcommand{\SENTii}{What did \NAMEiii \ call \NAMEi ?}
\newcommand{\SENTiii}{Does \NAMEiv \ like hanging out with \NAMEi ?}

\makeatletter
\newcommand{\randomize}[2]% #1=text part of csname, #2=number in list
{\bgroup% use local registers & definitions
  \count1=1
  \loop\ifnum\count1<#2
    \pgfmathparse{int(random(\the\count1,#2))}%
    \count2=\pgfmathresult\relax
    \expandafter\let\expandafter\tempa\csname #1\@roman\count1\endcsname
    \expandafter\let\expandafter\tempb\csname #1\@roman\count2\endcsname
    \global\expandafter\let\csname #1\@roman\count1\endcsname=\tempb
    \global\expandafter\let\csname #1\@roman\count2\endcsname=\tempa
    \advance\count1 by 1
  \repeat
\egroup}%
\makeatother

%\AtBeginDocument{\pgfmathsetseed{\time}}% doesn't seem to change

\begin{document}

\randomize{SENT}{3}

\SENTi

\SENTii

\SENTiii

\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.