Restrict an output of Mersenne Twister to 16 bits instead of 32 bits

Dear all
I want to use MT as a random number generator in my simulation which is consisted a sensor with a limit memory.
I want to convert the output of RNG of 32 bits to 16 bits. Dose anybody know how can I do it in MT.
Any help would be appreciated.
Map the 32-bit pseudo-random numbers generated by std::mt19937 to a uniformly distributed 16-bit range?

If a class is needed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <random>
#include <cstdint>
#include <ctime>

struct mt19937_16 : private std::mt19937
{
    using result_type = std::uint_fast16_t ;

    using std::mt19937::seed ;
    using std::mt19937::discard ;
    // more using ... (if required)

    explicit mt19937_16( std::uint_fast32_t s ) : std::mt19937(s) {}
    template< typename SEED_SEQUENCE > explicit mt19937_16( SEED_SEQUENCE& ss ) : std::mt19937(ss) {}


    static constexpr result_type min() { return 0 ; }
    static constexpr result_type max() { return 0xffff ; }

    result_type operator()() { return narrow( base() ) ; }

    private:
        std::mt19937& base() { return *this ; }
        const std::mt19937& base() const { return *this ; }

        static std::uniform_int_distribution<std::uint_fast16_t> narrow ;

    friend bool operator== ( const mt19937_16& a, const mt19937_16& b )
    { return a.base() == b ; }
    friend bool operator!= ( const mt19937_16& a, const mt19937_16& b )
    { return !( a == b ) ; }

    template< typename C, typename T >
    friend std::basic_ostream<C,T>& operator<< ( std::basic_ostream<C,T>& stm, const mt19937_16& mt16 )
    { return  stm << mt16.base() ; }

    template< typename C, typename T >
    friend std::basic_istream<C,T>& operator>> ( std::basic_istream<C,T>& stm, mt19937_16& mt16 )
    { return  stm >> mt16.base() ; }
};

std::uniform_int_distribution<std::uint_fast16_t> mt19937_16::narrow( 0, 0xffff ) ; // in the implementation file (cpp)

int main()
{
    mt19937_16 twister16( std::time(nullptr) ) ;

    for( int i = 0 ; i < 10 ; ++i ) std::cout << twister16() << '\n' ;

    std::normal_distribution<double> normal( 100.0, 25.0 ) ;
    for( int i = 0 ; i < 10 ; ++i ) std::cout << normal(twister16) << '\n' ;
}

http://coliru.stacked-crooked.com/a/51eaefb17c7becc2
Thank you for your reply.
Is 16bits also has a uniform distribution?
sorry for these questions :
1) If so, then why normal distribution here and what means?
1
2
    std::normal_distribution<double> normal( 100.0, 25.0 ) ;
    for( int i = 0 ; i < 10 ; ++i ) std::cout << normal(twister16) << '\n' ;

2) mt19937_16 means generate uniform random number (16 bits)?
3) Is it possible for you to give me a rough idea behind this code?
Thanks
> 16bits also has a uniform distribution?

std::uniform_int_distribution<> is a template, parameterised on a standard integer type.


> If so, then why normal distribution here and what means?

The normal normal distribution was put in for testing purposes; to verify that mt19937_16 could be used as a conforming uniform random number generator.


> mt19937_16 means generate uniform random number (16 bits)?

Yes.


> Is it possible for you to give me a rough idea behind this code?

Tutorial: http://isocpp.org/files/papers/n3551.pdf
Thanks
Topic archived. No new replies allowed.