0

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?

Thanks.

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

0

3 Answers 3

0

A humble start.

\documentclass[]{article}
\usepackage{tikz}
\begin{document}

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

\pgfmathsetseed{6}

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

\end{document}

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

2
  • 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
0

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

%% Useful TeX macros

\def\nul{\noexpand\nul}
\def\gobble#1{}

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

% Not necessary
\def\map#1#2{%
    \mapA{#1}#2\nul%
}
\def\mapA#1#2{%
    \ifx\nul#2%
        \let\next=\gobble%
    \else%
        #1{#2}%
        \let\next=\mapA%
    \fi%
    \next{#1}%
}

\def\commap#1#2{%
    \commapA{#1}#2,\nul,%
}
\def\commapA#1#2,{%
    \ifx\nul#2%
        \let\next=\gobble%
    \else%
        #1{#2}%
        \let\next=\commapA%
    \fi%
    \next{#1}%
}

%% Macros for randomness

\let\mappingmethod=\commap
\newcount\countA

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

% Gets length of map
\def\maplen#1{%
    \csname m@#1@len\endcsname%
}
% Gets element from map at index
\def\indexmap#1#2{%
    \csname m@#1@#2\endcsname%
}
% Gets random element from map
\def\randindexmap#1{%
    \indexmap{#1}{\the\numexpr\pdfuniformdeviate\maplen{#1}+1\relax}
}

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

\def\declen{3}
% Generates real random number whose integer part is in [#1, #2], and the decimal length is <= \declen
\def\rrand#1#2{%
    \irand{#1}{#2}%
    {\edef\declenA{\irand0\declen}
    \ifnum\declenA>0 %
        .%
        \repeated{\numexpr\declenA-1\relax}{\irand09}%
        \irand19%
    \fi}%
}

%% Creating macros which will actually be used

\createmap{spaceunits}{mm,cm,m,km}
\createmap{timeunits}{ms,s,m,h}
% \randsu -> generate random space unit
\def\randsu{{\rm\randindexmap{spaceunits}}}
% \randtu -> generate random time unit
\def\randtu{{\rm\randindexmap{timeunits}}}

%% Example
$\rrand1{10}\cdot\rrand0{10}\frac\randsu\randtu$

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.

4
  • 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
0

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 .