# Evaluating expressions using xfp

I want to generate two integer random numbers for the radius (r) and heigth (h) of a cilinder, in a certain question of my exam. I am using the package xfp to generate the random numbers. However, I would like to determine the volume of this cilinder, in function of these randomic values. I have noted that \FPeval{}{} does not work when I have random numbers. There is a way to store these values in a variable using package xfp? Below, my code:

\documentclass{article}
\usepackage{xfp}

\begin{document}

The value for the radius of a cilinder is equal to $r=\fpeval{randint(1,10)}$.
Supose that the height is equal to $h=\fpeval{randint(1,10)}$.
Then the volume of this solid is equal to $V=\pi r^2=.....$
(calculate the volume automatically)

\end{document}
'''

• Is switching to LuaLaTeX an option for you?
– Mico
Commented May 10, 2021 at 4:16

You can store the values in variables and use them in the calculations:

\documentclass{article}
\usepackage{xfp}

\begin{document}

\edef\cylinderheight{\fpeval{randint(1,10)}}% Cylinder height

The value for the radius of a cylinder is equal to $r = \cylinderradius$m.
Suppose that the height is equal to $h = \cylinderheight$m.
Then the volume of this solid is equal to $V = \pi r^2 h = \fpeval{round(pi * \cylinderradius^2 * \cylinderheight, 2)}$m\textsuperscript{3}.

\end{document}


If employing LuaLaTeX is an option for you, then the following solution may be of interest.

\documentclass{article}
\usepackage{luacode} % for '\luaexec' macro
\newcommand\myprint[1]{\luaexec{tex.sprint(#1)}}

\begin{document}
% First, generate two integer-valued random numbers "x", 1 \le x \le 10
\luaexec{ my_r = math.random(10) ; my_h = math.random (10) }

Suppose the radius and height of a cylinder are equal to
$r=\myprint{my_r}$ and $h=\myprint{my_h}$.
Then the volume of this solid is equal to
$V=\pi h r^2=\myprint{string.format("\%.3f", math.pi*my_h*my_r^2)}$.

\end{document}