Skip to content

Commit

Permalink
core: Add a UniformRandomVariable wrapper to meet UniformRandomBitGen…
Browse files Browse the repository at this point in the history
…erator requirements
  • Loading branch information
stavallo authored and Stefano Avallone committed Jan 15, 2024
1 parent 28a2f22 commit 2c8c6cc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ set(header_files
model/type-name.h
model/type-traits.h
model/uinteger.h
model/uniform-random-bit-generator.h
model/unused.h
model/valgrind.h
model/vector.h
Expand Down
84 changes: 84 additions & 0 deletions src/core/model/uniform-random-bit-generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2023 Universita' degli Studi di Napoli Federico II
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Stefano Avallone <[email protected]>
*/
#ifndef UNIFORM_RANDOM_BIT_GENERATOR_H
#define UNIFORM_RANDOM_BIT_GENERATOR_H

#include "random-variable-stream.h"

#include <limits>

namespace ns3
{

/**
* Wraps a UniformRandomVariable into a class that meets the requirements of a
* UniformRandomBitGenerator as specified by the C++11 standard, thus allowing
* the usage of ns-3 style UniformRandomVariables as generators in the functions
* of the standard random library.
*/
class UniformRandomBitGenerator
{
public:
UniformRandomBitGenerator()
: m_rv(CreateObject<UniformRandomVariable>())
{
}

/**
* \return the ns-3 style uniform random variable
*/
Ptr<UniformRandomVariable> GetRv() const
{
return m_rv;
}

/// Typedef needed to meet requirements. Result type must be an unsigned integer type.
using result_type = uint32_t;

/**
* \return the smallest value that operator() may return
*/
static constexpr result_type min()
{
return 0;
}

/**
* \return the largest value that operator() may return
*/
static constexpr result_type max()
{
return std::numeric_limits<result_type>::max();
}

/**
* \return a value in the closed interval [min(), max()]
*/
result_type operator()()
{
return m_rv->GetInteger(min(), max());
}

private:
Ptr<UniformRandomVariable> m_rv; //!< ns-3 style uniform random variable
};

} // namespace ns3

#endif /* UNIFORM_RANDOM_BIT_GENERATOR_H */

0 comments on commit 2c8c6cc

Please sign in to comment.