Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Let's say you need a range S1 = [0, k) | (k, N].
Use a distribution in the range S2 = [0, N - 1]. And then transform the result using the mapping f: S2 -> S1.
In your case, f is a function:
f(x) = if (x > = 3) x + 1 else x
mt19937 gen(time(0));
uniform_int_distribution<int> uid(1, 9);
int f(int n) {
if (n >= 3)
return n + 1;
else
return n;
}
int i = f(uid(gen));
I see two options:
- generate a number, check whether it is equal to "3" or not in the area "3" and regenerate if necessary. it's all in a cycle.
-- a sub-variant of the first option, immediately generate a large sequence and N elements, and when you take them from there, skip invalid ones, as the process has ended or there is free time for the process to generate new ones
- and the second option. Randomly choose the subrange [1, 3) or (3,10], given that they are of different sizes, and already get a number in it.
The second option gives a constant running time, albeit with a large average.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question