A
A
avion2018-11-16 00:17:10
C++ / C#
avion, 2018-11-16 00:17:10

PSN generation area in C++?

How to make the generation area be, for example, from 1 to 10, not including 3?

mt19937 gen(time(0));
uniform_int_distribution<int> uid(1, 10);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Moseychuk, 2018-11-16
@fshp

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));

V
Vasily Melnikov, 2018-11-16
@BacCM

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 question

Ask a Question

731 491 924 answers to any question