Skip to content

include/tenzr/modules/random/generator.h

Namespaces

Name
tenzr
tenzr::random

Classes

Name
class tenzr::random::Generator
Mutex-protected pseudo-random number generator built on a 64-bit Mersenne twister.

Source code

// Copyright 2026 The Tenzr Project Authors
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TENZR_MODULES_RANDOM_GENERATOR_H_
#define TENZR_MODULES_RANDOM_GENERATOR_H_

#include <cstdint>
#include <mutex>
#include <random>

namespace tenzr::random {

  class Generator {
   public:
    Generator();
    explicit Generator(uint64_t seed);

    void ManualSeed(uint64_t seed);
    void SeedRandom();

    uint64_t CurrentSeed() const { return initial_seed_; }

    uint64_t NextUint64();
    double NextDouble();  // Uniform [0.0, 1.0)
    float NextFloat();    // Uniform [0.0f, 1.0f)

    std::mt19937_64& Engine() { return engine_; }

   private:
    uint64_t initial_seed_{0};
    std::mt19937_64 engine_;
    mutable std::mutex mutex_;
  };

  Generator& DefaultGenerator();

}  // namespace tenzr::random

#endif  // TENZR_MODULES_RANDOM_GENERATOR_H_