I would like to create a exam class such as:

  1. I could include random generator of both numbers and units for conversion factors in physics and chemistry. In particular, what I wish is a pseudorandom generator code that allowed me to generate this output:

    $ 3.34\cdot 10^{-5}\dfrac{m}{s}$ to $\dfrac{km}{h}$ 

    (the random variables should be the figures, the power of ten AND the units in the fractions.

  2. I could include a random generator of chemical compounds (of course, as it happens with units, I can not in principle create a random generator of chemical compounds). What I want is to create a list of chemical compounds arranged by formula and/or name. E.g.:

    a) $Fe_{2}O_{3}$
    b) Chromium(III) oxide

    Any idea or suggestion of how could I create them? Any package/s I should create or check?

Post edit: should I created a whole randomgenerator list with pgf and the tikz package for every case?


Post edit(II): is not latex working on the equations in this stack section?


3 Answers 3


A humble start.


% See "94.3.6 Pseudo-random functions" and 
  % "95.3 Pseudo-Random Numbers" of https://ctan.org/pkg/pgf


    \item[Between 0 and 1, uniform] \pgfmathparse{rnd}\pgfmathresult
    \item[Between $-1$ and 1, uniform] \pgfmathparse{rand}\pgfmathresult


enter image description here enter image description here (https://ctan.org/pkg/pgf)

  • So, the issue is to create a macro with the item list, is that right? But I don't get how could i get e.g., {number+S.I.unit} for the mathematical stuff from the pseudorandom generator, and {chemical compound} using my favourite chem packages. A small example could be useful for me...Should I be more explicit? I will rewrite my question for details...
    – riemannium
    Oct 23, 2022 at 4:59
  • @riemannium Sorry, I am busy at the moment (working on a public talk). Oct 24, 2022 at 1:30

This is a plain pdfTeX solution using the \pdfuniformdeviate primitive from pdfTeX to generate uniformly distributed random integers.

%% Useful TeX macros


    \ifnum#1>0 %
        \expandafter\repeated\expandafter{\the\numexpr #1-1\relax}{#2}%

% Not necessary


%% Macros for randomness


% \createmap{<name>}{<mapable elements>} -> creates an indexable map of the <mapable elements>
        \advance\countA by 1\relax%
        \expandafter\def\csname m@#1@\the\countA\endcsname{##1}%
    \expandafter\edef\csname m@#1@len\endcsname{\the\countA}%

% Gets length of map
    \csname m@#1@len\endcsname%
% Gets element from map at index
    \csname m@#1@#2\endcsname%
% Gets random element from map

% Generates random number in [#1, #2]
    \the\numexpr\pdfuniformdeviate\numexpr #2-#1+1\relax+#1\relax%

% Generates real random number whose integer part is in [#1, #2], and the decimal length is <= \declen
    \ifnum\declenA>0 %

%% Creating macros which will actually be used

% \randsu -> generate random space unit
% \randtu -> generate random time unit

%% Example

A rundown of what's happening here:

  • \createmap takes two parameters: a name and comma separated values and creates from this a "hasmap" of sorts of the specified names and values (the keys are the indexes of the values).

    Instead of comma separated values, if you set \mappingmethod to \map you can group the values instead (eg. \createmap{example}{{first}{second}{third}}), this is useful if the values contain commas themselves).

  • \maplen gives you the size of the map (the number of values), \indexmap indexes a map by the given index (the second parameter), \randomindexmap indexes a map randomly.

  • \irand gives a random integer in the specified range (inclusive). While \rrand gives a random real number where the integer part is in the specified range (inclusive) and the number of numbers after the decimal point is given by \declen.

So we then create two maps, spaceunits and timeunits which have different spatial and temporal units, and macros \randsu and \randtu which index these maps randomly for convenience (\randsu gives a random spatial unit and \randtu gives a random temporal unit).

You can probably use \createmap and \randomindexmap for your second question.

  • After adding \bye, this still don't compile. Undefined control sequence. l.96 $\rrand1{10}\cdot\rrand0{10}\frac. What is \frac? This isn't a TeX command. After replacing it by "\over", I obtain "6.7∙4.67" at numerator and "kmh" at denominator. Am I missing something?
    – quark67
    Mar 22, 2023 at 20:36
  • @quark67 I assumed that whoever was using this was using LaTeX, but you can define it yourself with \def\frac#1#2{{{#1}\over{#2}}} (you need to properly group the numerator and denominator)
    – Slurp
    Mar 22, 2023 at 21:20
  • 1
    Ok, I though you provides TeX code because you speak about pdfTeX, not pdfLaTeX in your answser. And because there is no \documentclass{...}... \end{document}. Sorry for the mistake. It's probably better to add \documentclass{...}, \begin{document}, \end{document}` to avoid any confusion.
    – quark67
    Mar 22, 2023 at 23:29
  • You're totally right. I should be more consistent, thank you for your input!
    – Slurp
    Mar 22, 2023 at 23:30

I would suggest using LuaLaTeX for this task. You can program your logic for creating the "random" data for the problems in Lua, which is (imho) a much more approachable and complete language than anything LaTeX offers natively, and then typeset your questions with these random data.

You must log in to answer this question.

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