Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 37 additions & 25 deletions source/numerics.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,7 @@
class seed_seq;

// \ref{rand.util.canonical}, function template \tcode{generate_canonical}
template<class RealType, size_t bits, class URBG>
template<class RealType, size_t digits, class URBG>
RealType generate_canonical(URBG& g);

// \ref{rand.dist.uni.int}, class template \tcode{uniform_int_distribution}
Expand Down Expand Up @@ -3971,28 +3971,47 @@

\indexlibraryglobal{generate_canonical}%
\begin{itemdecl}
template<class RealType, size_t bits, class URBG>
template<class RealType, size_t digits, class URBG>
RealType generate_canonical(URBG& g);
\end{itemdecl}

\begin{itemdescr}
\pnum
\effects
Invokes \tcode{g()} $k$ times
to obtain values $g_0, \dotsc, g_{k-1}$, respectively.
Calculates a quantity
Let
\begin{itemize}
\item $r$ be \tcode{numeric_limits<RealType>::radix},
\item $R$ be $\tcode{g.max()} - \tcode{g.min()} + 1$,
\item $d$ be the smaller of
\tcode{digits} and \tcode{numeric_limits<RealType>::digits},
\begin{footnote}
$d$ is introduced to avoid any attempt
to produce more bits of randomness
than can be held in \tcode{RealType}.
\end{footnote}
\item $k$ be the smallest integer such that $R^k \ge r^d$, and
\item $x$ be $\left\lfloor R^k / r^d \right\rfloor$.
\end{itemize}
An \defn{attempt} is $k$ invocations of \tcode{g()}
to obtain values $g_0, \dotsc, g_{k-1}$, respectively,
and the calculation of a quantity
\[
S = \sum_{i=0}^{k-1} (g_i - \tcode{g.min()})
\cdot R^i
\]
using arithmetic of type
\tcode{RealType}.

\pnum
\effects
Attempts are made until $S < xr^d$.
\begin{note}
When $R$ is a power of $r$, precisely one attempt is made.
\end{note}

\pnum
\returns
$S / R^k$.
$\left\lfloor S / x \right\rfloor / r^d$.
\begin{note}
$0 \leq S / R^k < 1$.
The return value $c$ satisfies
$0 \leq c < 1$.
\end{note}

\pnum
Expand All @@ -4001,21 +4020,7 @@

\pnum
\complexity
Exactly
$k = \max(1, \left\lceil b / \log_2 R \right\rceil)$
invocations
of \tcode{g},
where $b$
\begin{footnote}
$b$ is introduced
to avoid any attempt
to produce more bits of randomness
than can be held in \tcode{RealType}.
\end{footnote}
is the lesser of \tcode{numeric_limits<RealType>::digits}
and \tcode{bits},
and
$R$ is the value of $\tcode{g.max()} - \tcode{g.min()} + 1$.
Exactly $k$ invocations of \tcode{g} per attempt.

\pnum
\begin{note}
Expand All @@ -4028,6 +4033,13 @@
into a value
that can be delivered by a random number distribution.
\end{note}

\pnum
\begin{note}
When $R$ is a power of $r$,
an implementation can avoid using an arithmetic type that is wider
than the output when computing $S$.
\end{note}
\end{itemdescr}

\indextext{random number generation!utilities|)}
Expand Down