0

I see that this question may have been answered here: Random using WELL512

However, it's not quite user friendly and doesn't provide an example how to use it in a 'real world' piece of code.

Here is what I currently have:

#define m (unsigned long)2147483647
#define q (unsigned long)127773    
#define a (unsigned int)16807
#define r (unsigned int)2836    

static unsigned long seed;    
void x_srandom(unsigned long initial_seed);
unsigned long x_random(void);    

void x_srandom(unsigned long initial_seed)
{
    seed = initial_seed;
}    

unsigned long x_random(void)
{
    int lo, hi, test;

    hi = (seed / q);
    lo = (seed % q);

    test = (a * lo - r * hi);

    if (test > 0)
        seed = test;
    else
        seed = (test + m);

    return (seed);
}

int RANDOM(int from, int to)
{
    if (from > to)
    {
        int tmp = from;
        from = to;
        to = tmp;
    }
    return ((x_random() % (to - from + 1)) + from);
}

// Real world function using RANDOM()
void testFunction()
{
    printf("A random number between 1 and 1000 is %d \r\n", RANDOM(1, 1000));
    printf("A random number between 36 and 100 is %d \r\n", RANDOM(36, 100));
    printf("A random number between 1 and 2147483647 is %d \r\n", RANDOM(1, 2147483647));
    printf("A random number between 1 and 5 is %d \r\n", RANDOM(1, 5));
}

The above example shows everything you need to know to implement it.

I would like to use WELL512 to determine my random numbers instead of the way in which I currently am, put in a way as exampled above.

1
  • Using modulo with anything but a power of two (like, 1000 or 36) will give a biased distribution. You should avoid doing that.
    – Damon
    Dec 13, 2014 at 21:25

4 Answers 4

3

It is really time to move away from using % for generating a distribution.

To me you should use WELL512 as a uniform random number generator (just like mt19937 in the standard library). You wrap it in a class that exposes a typedef (or using) for result_type. In your case that would probably be unsigned long. Then you need two constexpr for min() and max(). That would be 0 and ULONG_MAX. Finally you need to expose operator() that returns a single unsigned long.

After that you use the features in <random> together with your engine.

class well512 {
public:
    typedef unsigned long result_type;
    static constexpr result_type min() { return 0; }
    static constexpr result_type max() { return ULONG_MAX; }
    result_type operator()() { /* return some value from the underlying well512 implementation */ }
};

int main()
{
    well512 engine();
    std::uniform_int_distribution<> dist { 1, 5 };

    for (int i = 0; i != 10; ++i)
    {
        std::cout << dist(engine) << std::endl;
    }
    return 0;
}
1

Here is a complete example. It does not have all the bells and whistles you may want. E.g. there is no default constructor, or a constructor from a single word. I leave that as an exercise.

#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <numeric>
#include <ostream>
#include <random>
#include <vector>

class seed_seq
{
public:
    template <typename InputIterator>
    seed_seq(InputIterator first, InputIterator last)
    {
        for (; first != last; ++first)
        {
            v.push_back(*first);
        }
    }

    template <typename RandomAccessIterator>
    void generate(RandomAccessIterator first, RandomAccessIterator last)
    {
        std::vector<unsigned int>::size_type i = 0;
        for (; first != last; ++first)
        {
            *first = v[i];
            if (++i == v.size()){ i = 0; }
        }
    }

private:
    std::vector<unsigned int> v;
};

class well512
{
public:
    using result_type = unsigned int;

    static result_type min() { return 0; }
    static result_type max() { return std::numeric_limits<std::uint32_t>::max(); }

    static const unsigned int state_size = 16;

    explicit well512(seed_seq& sequence) : index(0)
    { sequence.generate(std::begin(state), std::end(state)); }

    result_type operator()()
    {
        std::uint32_t z0 = state[(index + 15) & 0x0fU];
        std::uint32_t z1 = xsl(16, state[index]) ^ xsl(15, state[(index + 13) & 0x0fU]);
        std::uint32_t z2 = xsr(11, state[(index + 9) & 0x0fU]);
        state[index] = z1 ^ z2;
        std::uint32_t t = xslm(5, 0xda442d24U, state[index]);
        index = (index + state_size - 1) & 0x0fU;
        state[index] = xsl(2, z0) ^ xsl(18, z1) ^ (z2 << 28) ^ t;

        return state[index];
    }

private:
    // xor-shift-right
    std::uint32_t xsr(unsigned int shift, std::uint32_t value)
    { return value ^ (value >> shift); }

    // xor-shift-left
    std::uint32_t xsl(unsigned int shift, std::uint32_t value)
    { return value ^ (value << shift); }

    // xor-shift-left and mask
    std::uint32_t xslm(unsigned int shift, std::uint32_t mask, std::uint32_t value)
    { return value ^ ((value << shift) & mask); }

    unsigned int index;
    std::array<std::uint32_t, state_size> state;
};

int main()
{
    // Use a random device to generate 16 random words used as seed for the well512 engine
    std::random_device rd;

    std::vector<well512::result_type> seed_data;
    std::generate_n(std::back_inserter(seed_data), well512::state_size, std::ref(rd));

    seed_seq sequence(std::begin(seed_data), std::end(seed_data));

    // Create a well512 engine
    well512 engine(sequence);

    // Now apply it like any other random engine in C++11
    std::uniform_int_distribution<> dist{ 1, 6 };
    auto rand = std::function <int()> { std::bind(std::ref(dist), std::ref(engine)) };

    // Print out some random numbers between 1 and 6 (simulating throwing a dice)
    const int n = 100;
    std::generate_n(std::ostream_iterator<int>(std::cout, " "), n, rand);
    std::cout << std::endl;

    return 0;
}
0

Like this user515430 ?

#define m  (unsigned long)2147483647

#define W 32
#define R 16
#define P 0
#define M1 13
#define M2 9
#define M3 5

#define MAT0POS(t,v) (v^(v>>t))
#define MAT0NEG(t,v) (v^(v<<(-(t))))
#define MAT3NEG(t,v) (v<<(-(t)))
#define MAT4NEG(t,b,v) (v ^ ((v<<(-(t))) & b))

#define V0            STATE[state_i                   ]
#define VM1           STATE[(state_i+M1) & 0x0000000fU]
#define VM2           STATE[(state_i+M2) & 0x0000000fU]
#define VM3           STATE[(state_i+M3) & 0x0000000fU]
#define VRm1          STATE[(state_i+15) & 0x0000000fU]
#define VRm2          STATE[(state_i+14) & 0x0000000fU]
#define newV0         STATE[(state_i+15) & 0x0000000fU]
#define newV1         STATE[state_i                 ]
#define newVRm1       STATE[(state_i+14) & 0x0000000fU]

#define FACT 2.32830643653869628906e-10

static unsigned int state_i = 0;
static unsigned int STATE[R];
static unsigned int z0, z1, z2;

void InitWELLRNG512a(unsigned int *init){
    int j;
    state_i = 0;
    for (j = 0; j < R; j++)
        STATE[j] = init[j];
}

double WELLRNG512a(void){
    z0 = VRm1;
    z1 = MAT0NEG(-16, V0) ^ MAT0NEG(-15, VM1);
    z2 = MAT0POS(11, VM2);
    newV1 = z1                  ^ z2;
    newV0 = MAT0NEG(-2, z0) ^ MAT0NEG(-18, z1) ^ MAT3NEG(-28, z2) ^ MAT4NEG(-5, 0xda442d24U, newV1);
    state_i = (state_i + 15) & 0x0000000fU;
    return ((double)STATE[state_i]) * FACT;
}

int RANDOM(int from, int to)
{
    if (from > to)
    {
        int tmp = from;
        from = to;
        to = tmp;
    }

    return to + (from - to) * (WELLRNG512a() / (long double)m);
}
1
  • Nevermind. This just outputs the largest number every time. RANDOM(1, 100) always results in 100. (back to the drawing board)
    – Zyre
    Dec 13, 2014 at 22:07
0

The simplest way to get a uniformly distributed random integer between two values is with floating point math.

double get_uniform_rand() {
    /*Assumes unsigned 32 bit return value from myrnd in range 0 - 0xFFFFFFFF*/
    return (double)myrnd() / (double)0xFFFFFFFF;
}

int32_t get_rnd_in_range(int32_t l, int32_t h) {
    return (int32_t)((double)l + get_uniform_rand() * (double)(h-l));
}

This is more of a C approach, as, like user515430 mentioned, there's a standard C++ way of doing this (though I personally have not used it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.