Bitstream Encoding Made Easy¶
A stochastic number is not “random junk” — it is a deliberate encoding of a real value as the statistics of a bit sequence.
Unipolar encoding¶
For \(x \in [0,1]\):
| Value \(x\) | Example length-8 pattern (one draw) | Mean |
|---|---|---|
| 0.00 | 00000000 |
0 |
| 0.25 | 10001000 |
0.25 |
| 0.50 | 10101010 |
0.50 |
| 1.00 | 11111111 |
1 |
Unipolar multiply (independent streams): \(X \land Y\) has mean \(x\cdot y\).
Bipolar encoding¶
For \(x \in [-1,1]\):
Bipolar multiply (independent streams): \(X \odot Y\) via XNOR yields mean \(\approx x\cdot y\) on the bipolar scale.
graph TD
U[Unipolar x in 0..1] --> AND[AND = multiply]
B[Bipolar x in -1..1] --> XNOR[XNOR = multiply]
U --> MUX[MUX / OR hybrids = scaled add]
B --> SCALE[Scaled add via multiplexers]
Generating streams (SNG sketch)¶
A common stochastic number generator:
- Draw a uniform random integer \(R_t\) each cycle (LFSR, TRNG, Sobol, …).
- Compare to a binary register holding \(x\).
- Output bit \(X_t = [R_t < x]\).
Deterministic variants replace \(R_t\) with a low-discrepancy sequence so short \(N\) still tracks \(x\) closely. See Number Generators.
Decoding¶
Hardware usually counts 1s over a window (or uses progressive counters) and optionally renormalizes. Window choice sets the precision–latency trade-off covered in Error, Precision & Latency.
Try it¶
The bitstream multiply lab on the “Why Probabilistic Computing?” page samples unipolar streams live in the browser.