LCOV - code coverage report
Current view: top level - buildbot/coverage/build/src/util - random.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 18 18 100.0 %
Date: 2026-09-27 09:33:07 Functions: 5 5 100.0 %
Branches: 6 14 42.9 %

           Branch data     Line data    Source code
       1                 :            : /******************************************************************************
       2                 :            :  * This file is part of the cvc5 project.
       3                 :            :  *
       4                 :            :  * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
       5                 :            :  * in the top-level source directory and their institutional affiliations.
       6                 :            :  * All rights reserved.  See the file COPYING in the top-level source
       7                 :            :  * directory for licensing information.
       8                 :            :  * ****************************************************************************
       9                 :            :  *
      10                 :            :  * A random number generator, based on the Mersenne-Twister engine.
      11                 :            :  */
      12                 :            : 
      13                 :            : #include "util/random.h"
      14                 :            : 
      15                 :            : #include "base/check.h"
      16                 :            : 
      17                 :            : namespace cvc5::internal {
      18                 :            : 
      19                 :      44158 : Random::Random(uint64_t seed)
      20                 :            : {
      21                 :            : #ifdef CVC5_GMP_IMP
      22                 :            :   gmp_randinit_mt(d_gmp_randstate);
      23                 :            : #endif
      24                 :      44158 :   setSeed(seed);
      25                 :      44158 : }
      26                 :            : 
      27                 :      39624 : Random::~Random()
      28                 :            : {
      29                 :            : #ifdef CVC5_GMP_IMP
      30                 :            :   gmp_randclear(d_gmp_randstate);
      31                 :            : #endif
      32                 :      39624 : }
      33                 :            : 
      34                 :      71267 : void Random::setSeed(uint64_t seed)
      35                 :            : {
      36         [ +  + ]:      71267 :   d_seed = seed == 0 ? ~seed : seed;
      37                 :      71267 :   d_rng.seed(d_seed);
      38                 :            : #ifdef CVC5_GMP_IMP
      39                 :            :   gmp_randseed_ui(d_gmp_randstate, d_seed);
      40                 :            : #endif
      41                 :            : #ifdef CVC5_CLN_IMP
      42                 :            :   // cln::random_state stores a 64 bit seed split into 32-bit hi and lo parts.
      43                 :      71267 :   d_cln_randstate.seed.hi = static_cast<uint32_t>(d_seed >> 32);
      44                 :      71267 :   d_cln_randstate.seed.lo = static_cast<uint32_t>(d_seed);
      45                 :            : #endif
      46                 :      71267 : }
      47                 :            : 
      48                 :      93333 : uint64_t Random::operator()() { return d_rng(); }
      49                 :            : 
      50                 :    9312393 : bool Random::pickWithProb(double probability)
      51                 :            : {
      52 [ -  + ][ -  + ]:    9312393 :   Assert(probability <= 1);
                 [ -  - ]
      53 [ -  + ][ -  + ]:    9312393 :   Assert(probability >= 0);
                 [ -  - ]
      54                 :    9312393 :   uint64_t p = (uint64_t)(probability * 1000);
      55                 :    9312393 :   uint64_t r = pick<uint64_t>(0, 999);
      56                 :    9312393 :   return r < p;
      57                 :            : }
      58                 :            : 
      59                 :            : }  // namespace cvc5::internal

Generated by: LCOV version 1.14