Files
2025-10-25 03:02:53 +03:00

3.1 KiB
Raw Permalink Blame History

[rand.util.canonical]

29 Numerics library [numerics]

29.5 Random number generation [rand]

29.5.8 Utilities [rand.util]

29.5.8.2 Function template generate_canonical [rand.util.canonical]

🔗

template<class RealType, size_t digits, class URBG> RealType generate_canonical(URBG& g);

1

#

Let

r be numeric_limits::radix,

R be g.max()−g.min()+1,

d be the smaller of digits and numeric_limits::digits,245

k be the smallest integer such that Rk≥rd, and

x be ⌊Rk/rd⌋.

An attempt is k invocations of g() to obtain values g0,…,gk−1, respectively, and the calculation of a quantity S given by Formula 29.1:

S=k−ˆ‘i=0(gi−g.min())â‹Ri(29.1)

2

#

Effects: Attempts are made until S<xrd.

[Note 1:

When R is a power of r, precisely one attempt is made.

— end note]

3

#

Returns: ⌊S/x⌋/rd.

[Note 2:

The return value c satisfies0≤c<1.

— end note]

4

#

Throws: What and when g throws.

5

#

Complexity: Exactly k invocations of g per attempt.

6

#

[Note 3:

If the values gi produced by g are uniformly distributed, the instantiation's results are distributed as uniformly as possible.

Obtaining a value in this way can be a useful step in the process of transforming a value generated by a uniform random bit generator into a value that can be delivered by a random number distribution.

— end note]

7

#

[Note 4:

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]

245)245)

d is introduced to avoid any attempt to produce more bits of randomness than can be held in RealType.