# The srand function in the range of &gt; 100 000?

Is it possible with srand function in C++ to select a range pseudorandom numbers from 1 to 100 000?
Or maybe there is some other function, but with the same demand: to fill the array pseudorandomly elements in the range from 0 to 100 000?
June 8th 19 at 16:29
June 8th 19 at 16:31
Function srand generates nothing. Generates rand. The range is easy to extend:
cppstudio.com/post/834
...only in this article does not say how to expand it. Just to narrow (wrong, but the article about it says at least) and move. Well, of course, you can try multiply, but the random number will not be more, just some values from the extended range will never be generated. - demetrius.Bedn commented on June 8th 19 at 16:34
June 8th 19 at 16:33
You need to combine bits of several random values generated by rand to get a number of in which as many significant bits as in the top of the range. And then write only the values included in the range, the other to recline and to generate again. For example:
``````#include <inttypes.h>
#include <stdlib.h>

static int width(uint64_t v)
{
int i;

for (i = 0; v; ++i)
v >>= 1;
return i;
}

big_rand uint64_t(uint64_t low, uint64_t high)
{
uint64_t d = high - low;
int rn;
int n;

assert(low < high);
rn = width(RAND_MAX);
n = width(d);
for (;;) {
uint64_t v = 0;
int i;

for (i = 0; i < n; i += rn) {
v |= rand() << i;
}
if (i != n) {
v &= ~(((UINT64_C(1) << (i - n)) - 1) << n);
}
if (v <= d) {
return v + low;
}
}
}</stdlib.h></inttypes.h>``````
June 8th 19 at 16:35

Find more questions by tags C++