Random Stream Tree<TRandom> Class
Definition
Assembly: Numerics.NET (in Numerics.NET.dll) Version: 10.3.0
public sealed class RandomStreamTree<TRandom> : RandomStreamSource<TRandom>
where TRandom : Object, IRandomSourceFactory<TRandom>
- Inheritance
- Object → RandomStreamSource<TRandom> → RandomStreamTree<TRandom>
Type Parameters
- TRandom
- The type of random number generator to produce.
Remarks
RandomStreamTree<TRandom> provides NumPy-style hierarchical stream generation where each stream is derived from base seed material combined with its hierarchical path. This approach ensures:
- Determinism: Same seed + same path always produces identical streams
- Independence: Different paths produce uncorrelated streams
- Unlimited depth: Hierarchy can be arbitrarily deep
- O(1) advancement: Advance(UInt64) is constant-time
NumPy Compatibility:
When using Numpy, single-level streams (paths with one element, e.g., [0], [1], [5]) produce bit-identical output to NumPy's SeedSequence.spawn() for compatible RNGs (PCG64, Philox, etc.). The path element corresponds directly to NumPy's spawn key.
For multi-level hierarchies (nested spawning), the current implementation uses a deterministic hash-based combination of path elements. This ensures reproducible behavior within Numerics.NET but may not produce bit-identical results to NumPy's nested spawn() calls. For bit-exact NumPy compatibility in nested scenarios, use single-level spawning and manually create RandomOptions with explicit stream IDs that match NumPy's spawn keys.
Hierarchical Addressing:
Each position in the hierarchy is addressed by a path of 64-bit indices.
For single-level NumPy-compatible spawning:
// Matches: np.random.SeedSequence(42).spawn(10)[3]
var tree = new RandomStreamTree<Pcg64>(
new RandomOptions(42, null, SeedProfile.Numpy));
var rng = tree.Branch(3).PrefixStream();
For nested hierarchies (deterministic but not bit-exact with NumPy):
var tree = new RandomStreamTree<Pcg64>(
new RandomOptions(42, null, SeedProfile.Numpy));
var child = tree.Branch(3);
var grandchild = child.Branch(2);
var rng = grandchild.PrefixStream();
Performance:
Stream creation cost is O(1) in the number of streams at the same level, but O(depth) in the hierarchy depth due to path-based derivation. For very deep hierarchies (depth > 10), consider caching commonly-used child scopes.
Example
var options = new RandomOptions(42, null, SeedProfile.Numpy);
var tree = new RandomStreamTree<Pcg64>(in options);
// Generate root-level streams
var rng0 = tree.NextStream(); // Path: [0]
var rng1 = tree.NextStream(); // Path: [1]
// Create nested hierarchy
var child = tree.Branch(5);
var rng5_0 = child.NextStream(); // Path: [5, 0]
var rng5_1 = child.NextStream(); // Path: [5, 1]
// Skip ahead efficiently
tree.Advance(100);
var rng102 = tree.NextStream(); // Path: [102]Constructors
| Random | Initializes a new instance of the RandomStreamTree<TRandom> class. |
Properties
| Next |
Gets the index of the next stream to be consumed at this scope.
(Inherited from RandomStreamSource<TRandom>) |
| Path |
Gets the hierarchical path identifying the current scope.
(Inherited from RandomStreamSource<TRandom>) |
Methods
| Advance |
Advances the next index by the specified count without generating streams.
(Overrides RandomStreamSource<TRandom>.Advance(UInt64)) |
| Branch |
Creates a child stream source at the specified index without advancing this source.
(Inherited from RandomStreamSource<TRandom>) |
| Branch |
Creates a child stream source at the current next index and advances this source.
(Inherited from RandomStreamSource<TRandom>) |
| Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
| Get | Serves as the default hash function. (Inherited from Object) |
| Get | Gets the Type of the current instance. (Inherited from Object) |
| Next |
Returns an RNG for the next child stream and advances the index.
(Overrides RandomStreamSource<TRandom>.NextStream()) |
| Next |
Returns an array of RNGs for the next N child streams and advances the index.
(Inherited from RandomStreamSource<TRandom>) |
| Next |
Fills the specified span with the next child streams and advances the index.
(Inherited from RandomStreamSource<TRandom>) |
| Prefix |
Returns an RNG corresponding to the current path prefix only.
(Overrides RandomStreamSource<TRandom>.PrefixStream()) |
| Seek |
Sets the next stream index to the specified value.
(Overrides RandomStreamSource<TRandom>.Seek(UInt64)) |
| ToString | Returns a string that represents the current object. (Inherited from Object) |