|
1371 | 1371 | class seed_seq;
|
1372 | 1372 |
|
1373 | 1373 | // \ref{rand.util.canonical}, function template \tcode{generate_canonical}
|
1374 |
| - template<class RealType, size_t bits, class URBG> |
| 1374 | + template<class RealType, size_t digits, class URBG> |
1375 | 1375 | RealType generate_canonical(URBG& g);
|
1376 | 1376 |
|
1377 | 1377 | // \ref{rand.dist.uni.int}, class template \tcode{uniform_int_distribution}
|
|
3971 | 3971 |
|
3972 | 3972 | \indexlibraryglobal{generate_canonical}%
|
3973 | 3973 | \begin{itemdecl}
|
3974 |
| -template<class RealType, size_t bits, class URBG> |
| 3974 | +template<class RealType, size_t digits, class URBG> |
3975 | 3975 | RealType generate_canonical(URBG& g);
|
3976 | 3976 | \end{itemdecl}
|
3977 | 3977 |
|
3978 | 3978 | \begin{itemdescr}
|
3979 | 3979 | \pnum
|
3980 |
| -\effects |
3981 |
| - Invokes \tcode{g()} $k$ times |
3982 |
| - to obtain values $g_0, \dotsc, g_{k-1}$, respectively. |
3983 |
| - Calculates a quantity |
| 3980 | +Let |
| 3981 | +\begin{itemize} |
| 3982 | +\item $r$ be \tcode{numeric_limits<RealType>::radix}, |
| 3983 | +\item $R$ be $\tcode{g.max()} - \tcode{g.min()} + 1$, |
| 3984 | +\item $d$ be the smaller of |
| 3985 | + \tcode{digits} and \tcode{numeric_limits<RealType>::digits}, |
| 3986 | + \begin{footnote} |
| 3987 | + $d$ is introduced to avoid any attempt |
| 3988 | + to produce more bits of randomness |
| 3989 | + than can be held in \tcode{RealType}. |
| 3990 | + \end{footnote} |
| 3991 | +\item $k$ be the smallest integer such that $R^k \ge r^d$, and |
| 3992 | +\item $x$ be $\left\lfloor R^k / r^d \right\rfloor$. |
| 3993 | +\end{itemize} |
| 3994 | +An \defn{attempt} is $k$ invocations of \tcode{g()} |
| 3995 | +to obtain values $g_0, \dotsc, g_{k-1}$, respectively, |
| 3996 | +and the calculation of a quantity |
3984 | 3997 | \[
|
3985 | 3998 | S = \sum_{i=0}^{k-1} (g_i - \tcode{g.min()})
|
3986 | 3999 | \cdot R^i
|
3987 | 4000 | \]
|
3988 |
| - using arithmetic of type |
3989 |
| - \tcode{RealType}. |
| 4001 | + |
| 4002 | +\pnum |
| 4003 | +\effects |
| 4004 | +Attempts are made until $S < xr^d$. |
| 4005 | +\begin{note} |
| 4006 | +When $R$ is a power of $r$, precisely one attempt is made. |
| 4007 | +\end{note} |
3990 | 4008 |
|
3991 | 4009 | \pnum
|
3992 | 4010 | \returns
|
3993 |
| -$S / R^k$. |
| 4011 | +$\left\lfloor S / x \right\rfloor / r^d$. |
3994 | 4012 | \begin{note}
|
3995 |
| -$0 \leq S / R^k < 1$. |
| 4013 | +The return value $c$ satisfies |
| 4014 | +$0 \leq c < 1$. |
3996 | 4015 | \end{note}
|
3997 | 4016 |
|
3998 | 4017 | \pnum
|
|
4001 | 4020 |
|
4002 | 4021 | \pnum
|
4003 | 4022 | \complexity
|
4004 |
| -Exactly |
4005 |
| - $k = \max(1, \left\lceil b / \log_2 R \right\rceil)$ |
4006 |
| - invocations |
4007 |
| - of \tcode{g}, |
4008 |
| - where $b$ |
4009 |
| -\begin{footnote} |
4010 |
| -$b$ is introduced |
4011 |
| - to avoid any attempt |
4012 |
| - to produce more bits of randomness |
4013 |
| - than can be held in \tcode{RealType}. |
4014 |
| -\end{footnote} |
4015 |
| - is the lesser of \tcode{numeric_limits<RealType>::digits} |
4016 |
| - and \tcode{bits}, |
4017 |
| - and |
4018 |
| - $R$ is the value of $\tcode{g.max()} - \tcode{g.min()} + 1$. |
| 4023 | +Exactly $k$ invocations of \tcode{g} per attempt. |
4019 | 4024 |
|
4020 | 4025 | \pnum
|
4021 | 4026 | \begin{note}
|
|
4028 | 4033 | into a value
|
4029 | 4034 | that can be delivered by a random number distribution.
|
4030 | 4035 | \end{note}
|
| 4036 | + |
| 4037 | +\pnum |
| 4038 | +\begin{note} |
| 4039 | +When $R$ is a power of $r$, |
| 4040 | +an implementation can avoid using an arithmetic type that is wider |
| 4041 | +than the output when computing $S$. |
| 4042 | +\end{note} |
4031 | 4043 | \end{itemdescr}
|
4032 | 4044 |
|
4033 | 4045 | \indextext{random number generation!utilities|)}
|
|
0 commit comments