• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ Biased Random Number Generator (min, max)

Lyky

Well-Known Member
Joined
May 27, 2014
Messages
291
Solutions
8
Reaction score
89
Hi,
I would like to request a biased random number generator, by my understanding it would have to be placed in tools.cpp (below int32_t normal_random)


Where as example the call would be
minNumber = 1
maxNumber = 10

result
1 = 60%
2 = 80%
3 = 45%
4 = 35%
5 = 20%
6 = 10%
7 = 1%
8 = 0.5%
9 = 0.1%
10 = 0.01%

obviously with numbers are vars, i was never good at math - and i think it would need gaussian.
(if it makes things easier maybe without gauss, and just chance going down uniformly down 1=80% -> 10=0.01% -> 100=0.0001% or so )

Thanks & Regards,
Lyky
 
Last edited:
I would prefer to use something that already present in boost random lib. (i.e. not to reinvent the wheel.)
in terms of most similar to accomplish my goal would be the use std::exponential_distribution

so i did something like that in tools

tools.h line #52
C++:
int32_t biased_random(int32_t minNumber, int32_t maxNumber);

tools.cpp line #315 -- under uniformRand generator
I assumed biasRand(3.5f) is going to be lambda.

C++:
int32_t biased_random(int32_t minNumber, int32_t maxNumber)
{
    static std::exponential_distribution<float> biasRand(3.5f);
    if (minNumber == maxNumber) {
        return minNumber;
    }
    else if (minNumber > maxNumber) {
        std::swap(minNumber, maxNumber);
    }
    return biased_random(getRandomGenerator(), std::exponential_distribution<int32_t>::param_type(minNumber, maxNumber));
}

but i have issues with return
return biased_random(getRandomGenerator(), std::exponential_distribution<int32_t>::param_type(minNumber, maxNumber));
1590162882873.png


I assume i can't do that; what would be the correct way to do the return for exponential_distribution.
 
From the OP it sounds like you really only need to copy the code below "Generating integers with different probabilities" that I linked. An example is a weighted die. You put in your probabilities, and you call the function..
 
From the OP it sounds like you really only need to copy the code below "Generating integers with different probabilities" that I linked. An example is a weighted die. You put in your probabilities, and you call the function..
Apologies, but op 10 numbers there were just example, i want numbers to scale between defined min, max that can be different - and sometimes span over 100; If I read this method right, i would need to define probability for each number manually. It would work if my min max range was always within 1 to 10, but if range min, max between 1, 100 then from the looks it wouldn't scale with this just specified values here?

double probabilities[] = {
0.5, 0.1, 0.1, 0.1, 0.1, 0.1
};
Post automatically merged:

This seems to work for me.
C++:
int32_t biased_random(int32_t minNumber, int32_t maxNumber)
{
    static std::exponential_distribution<float> biasRand(6.0f);
    if (minNumber == maxNumber) {
        return minNumber;
    }
    else if (minNumber > maxNumber) {
        std::swap(minNumber, maxNumber);
    }

    int32_t increment;
    const int32_t diff = maxNumber - minNumber;
    const float v = biasRand(getRandomGenerator());
    if (v < 0.0) {
        increment = diff / 2;
    }
    else if (v > 1.0) {
        increment = (diff + 1) / 2;
    }
    else {
        increment = round(v * diff);
    }
    return minNumber + increment;
}
 

Attachments

  • 1590277272864.png
    1590277272864.png
    152.9 KB · Views: 31 · VirusTotal
Last edited:
Back
Top