How to have a fixed value in a command

In plain TeX, you can use \input random. Define \preprandom for changing the result and \result prints the randomly chosen result:

\input random

\newcount\rann
\newcount\tmpnum

\def\preprandom{\setrannum\rann{1}{30}}
\def\result{\ifcase\rann\or
  Francesco\or Alessandro\or Mattia\or Lorenzo\or Leonardo\or
  Andrea\or Gabriele\or Matteo\or Tommaso\or Riccardo\or Davide\or
  Giuseppe\or Edoardo\or Antonio\or Federico\or Giovanni\or Marco\or
  Diego\or Samuele\or Pietro\or Christian\or Nicolò\or Luca\or Simone\or 
  Filippo\or Alessio\or Gabriele\or Michele\or Emanuele\or Jacopo\fi
}

\tmpnum=0
\loop
   \preprandom 
   \result\endgraf \result\endgraf \result\endgraf \medskip\hrule\medskip
   \advance\tmpnum by1
   \ifnum\tmpnum<5 \repeat

\bye

How about pre-storing the full list of names in a \seq variable first and use \seq_rand_item:N which is a 'new' feature (introduced on 2016/12/06) and is expandable, i.e. its expansion value can be stored to some other macro, say \edef\namestored{\name}

\documentclass[11pt, a4paper]{article}

\usepackage[italian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgffor}
\usepackage{xparse}



\ExplSyntaxOn

\seq_new:N \l_candidates_name_seq

\NewDocumentCommand{\StoreNames}{m}{%
  \seq_set_from_clist:Nn \l_candidates_name_seq {#1}
}

\cs_new:Npn \selectNrandom #1 {%
  \seq_rand_item:N \l_candidates_name_seq 
}

\newcommand{\name}{\selectNrandom{1}}

\ExplSyntaxOff

\StoreNames{Francesco, Alessandro, Mattia, Lorenzo, Leonardo, Andrea, Gabriele, Matteo, Tommaso, Riccardo, Davide, Giuseppe, Edoardo, Antonio, Federico, Giovanni, Marco, Diego, Samuele, Pietro, Christian, Nicolò, Luca, Simone, Filippo, Alessio, Gabriele, Michele, Emanuele, Jacopo}



\begin{document}


\foreach \pagenum in {1,...,5}{
  \edef\namestored{\name}

  \namestored

  \namestored

  \namestored
  \medskip
  \hrule
  \medskip
   %\pagebreak

}

\end{document}

Due to its random nature the output of the run will change (unless the random seed is fixed)

enter image description here

Storing with \cs_new:Npx \namestored:

\documentclass[11pt, a4paper]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{pgffor}
\usepackage{xparse}



\ExplSyntaxOn

\seq_new:N \l_candidates_name_seq

\NewDocumentCommand{\StoreNames}{m}{%
  \seq_set_from_clist:Nn \l_candidates_name_seq {#1}
}

\cs_new:Npn \selectNrandom #1 {%
  \seq_rand_item:N \l_candidates_name_seq 
}

\cs_new:Npn \namex {
  \cs_set:Npx \namestored {\seq_rand_item:N \l_candidates_name_seq }
}


\newcommand{\name}{\selectNrandom{1}}

\ExplSyntaxOff

\StoreNames{Francesco, Alessandro, Mattia, Lorenzo, Leonardo, Andrea, Gabriele, Matteo, Tommaso, Riccardo, Davide, Giuseppe, Edoardo, Antonio, Federico, Giovanni, Marco, Diego, Samuele, Pietro, Christian, Nicolò, Luca, Simone, Filippo, Alessio, Gabriele, Michele, Emanuele, Jacopo}



\begin{document}


\foreach \pagenum in {1,...,5}{
  \namex

  \namestored

  \namestored

  \namestored
  \medskip
  \hrule
  \medskip

}

\end{document}

Tags:

Random

Macros