Skip to main content
Fixed range
Source Link
Lukas
  • 136
  • 1
  • 1
  • 5

Starting with C++11 you should no longer use rand() as RNG. For more background information see S.T.L.'s "rand() Considered Harmful" talk from 2013.

Instead, you want to use the <random> header with std::uniform_real_distribution.

#include <random>
#include <iostream>

class RandomNumberGenerator
{
    std::random_device m_randomDevice{};
    std::mt19937 m_engine{m_randomDevice()};

public:
    // Generates a random float in the range [low, high]high)
    float Generate(float low, float high)
    {
        return std::uniform_real_distribution<float>{low, high}(m_engine);
    }
};

int main()
{
    auto rng = RandomNumberGenerator{};

    for (auto i = 0u; i < 20u; ++i)
    {
        std::cout << rng.Generate(1.2f, 10.4f) << "\n";
    }
}

Starting with C++11 you should no longer use rand() as RNG. For more background information see S.T.L.'s "rand() Considered Harmful" talk from 2013.

Instead, you want to use the <random> header with std::uniform_real_distribution.

#include <random>
#include <iostream>

class RandomNumberGenerator
{
    std::random_device m_randomDevice{};
    std::mt19937 m_engine{m_randomDevice()};

public:
    // Generates a random float in the range [low, high]
    float Generate(float low, float high)
    {
        return std::uniform_real_distribution<float>{low, high}(m_engine);
    }
};

int main()
{
    auto rng = RandomNumberGenerator{};

    for (auto i = 0u; i < 20u; ++i)
    {
        std::cout << rng.Generate(1.2f, 10.4f) << "\n";
    }
}

Starting with C++11 you should no longer use rand() as RNG. For more background information see S.T.L.'s "rand() Considered Harmful" talk from 2013.

Instead, you want to use the <random> header with std::uniform_real_distribution.

#include <random>
#include <iostream>

class RandomNumberGenerator
{
    std::random_device m_randomDevice{};
    std::mt19937 m_engine{m_randomDevice()};

public:
    // Generates a random float in the range [low, high)
    float Generate(float low, float high)
    {
        return std::uniform_real_distribution<float>{low, high}(m_engine);
    }
};

int main()
{
    auto rng = RandomNumberGenerator{};

    for (auto i = 0u; i < 20u; ++i)
    {
        std::cout << rng.Generate(1.2f, 10.4f) << "\n";
    }
}
Source Link
Lukas
  • 136
  • 1
  • 1
  • 5

Starting with C++11 you should no longer use rand() as RNG. For more background information see S.T.L.'s "rand() Considered Harmful" talk from 2013.

Instead, you want to use the <random> header with std::uniform_real_distribution.

#include <random>
#include <iostream>

class RandomNumberGenerator
{
    std::random_device m_randomDevice{};
    std::mt19937 m_engine{m_randomDevice()};

public:
    // Generates a random float in the range [low, high]
    float Generate(float low, float high)
    {
        return std::uniform_real_distribution<float>{low, high}(m_engine);
    }
};

int main()
{
    auto rng = RandomNumberGenerator{};

    for (auto i = 0u; i < 20u; ++i)
    {
        std::cout << rng.Generate(1.2f, 10.4f) << "\n";
    }
}