Secure RandomInt Generator

Cryptographically secure random integer generator for producing uniformly distributed integers within a specified inclusive range.

This generator uses SecureRandomBytesGenerator (ChaCha20-based) as its entropy source and applies rejection sampling to guarantee an unbiased distribution even when the range does not evenly divide the underlying byte space.

Constructor- init(lower_bound: int, upper_bound: int)

Creates a new random integer generator bound to the inclusive interval [lower_bound, upper_bound]. The constructor validates the bounds, initializes a secure entropy source, and precomputes internal values required for unbiased rejection sampling.

Parameters
  • lower_bound (int) The smallest integer that may be generated. Must be strictly smaller than upper_bound.

  • upper_bound (int) The largest integer that may be generated. Must be strictly greater than lower_bound.

Methods

generate()

Generate a secure random integer within the configured range.

Uses rejection sampling to ensure every integer in the range [lower_bound, upper_bound] has equal probability.

Returns
  • int - A cryptographically secure uniformly distributed integer between lower_bound and upper_bound (inclusive).

·

©

2026

·

©

2026