Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why uniform_int_distribution does no produce all values?

16 views
Skip to first unread message

JiiPee

unread,
Feb 12, 2017, 5:14:51 PM2/12/17
to
I am trying to get values from the whole : 1 - max(unsigned long long)
range. But this code (which many recommend on the internet) just does
not seem to give small number never. the range it produces (128 numbers)
is always something like:


153348556926869012 - 18291104122139120324

(

153348556926869012

18291104122139120324

)

As you can see it keep giving ONLY big numbers. It never give numbers
like 9887. Why? How can I get values from the whole range? Thanks.


Here is the code I am using (mi and ma are the lower and upper range or
the results):

const int zobristSize = 128;

uint64_t zobrist[zobristSize];

std::random_device rd;

std::mt19937_64 e2(std::random_device{}()); // 2723506774205678741
std::uniform_int_distribution<unsigned long long> dist(1,
(std::numeric_limits<unsigned long long>::max)());

for (int n = 0; n < zobristSize; ++n) {
zobrist[n] = 0;
}
uint64_t mi = ((std::numeric_limits<unsigned long long>::max)() - 100);
uint64_t ma= 0;
for (int n = 0; n < zobristSize; ++n) {
// eatch value must not be before in the list
while (true)
{
zobrist[n] = dist(e2);
for (int i = 0; i < zobristSize; ++i)
if (i != n && zobrist[n] == zobrist[i])
continue;
break;
}
if (zobrist[n] < mi)
mi = zobrist[n];
if (zobrist[n] > ma)
ma = zobrist[n];
}

Alf P. Steinbach

unread,
Feb 12, 2017, 6:33:32 PM2/12/17
to
On 12.02.2017 23:14, JiiPee wrote:
> I am trying to get values from the whole : 1 - max(unsigned long long)
> range. But this code (which many recommend on the internet) just does
> not seem to give small number never. the range it produces (128 numbers)
> is always something like:
>
>
> 153348556926869012 - 18291104122139120324
>
> (
>
> 153348556926869012
>
> 18291104122139120324
>
> )
>
> As you can see it keep giving ONLY big numbers. It never give numbers
> like 9887. Why? How can I get values from the whole range? Thanks.

That looks like 64-bit numbers.

Now if you produce 1 such number, the chance of it being less than 2^16
= 65536, a not unreasonable choice for “small” number, is 2^16/2^64 =
2^(16-64) = 2^(-48). The Windows calculator tells me that that is
roughly 3,55 * 10^(-15). Now if you produce 1000 such numbers, the
chance of getting at least one small one, is at most 3,55 * 10^(-12).

More precisely, the chance of not getting any small number among the
1000 numbers is (1 - (3,55 * 10^(-15)))^1000, and correspondingly the
chance of getting at least one, is 1 - (1 - (3,55 * 10^(-15)))^1000. The
Windows calculator says that that is, roughly, 3,55 * 10^(-12). Oh!
Nothing much gained for getting precise here! :)


Cheers & hth.,

- Alf

Öö Tiib

unread,
Feb 12, 2017, 6:37:06 PM2/12/17
to
On Monday, 13 February 2017 00:14:51 UTC+2, JiiPee wrote:
> I am trying to get values from the whole : 1 - max(unsigned long long)
> range. But this code (which many recommend on the internet) just does
> not seem to give small number never.

I did not look at your code since the behavior as you describe feels
totally as expected to me.

Same happens when we try to count from unsigned long long max to 0,
it will seemingly give 42 never. The actual problem is that it would
take about 150 years to reach 42 when counting at 4 gigahertz one per
cycle.

Random number generators take tens of cycles per number so to get
for example one of 0 to 150 (from uniform distribution between 0
and unsigned long long max) would likely take tens of years of
generating. We accept 0 to 300? Then it would be expected in twice
less than in tens of years. :D

Oh and beware of random_device, it is not guaranteed to work by
standard. Implementation may choose that it does nothing.

JiiPee

unread,
Feb 12, 2017, 6:43:59 PM2/12/17
to
"the windows calculator says" ... haha. so that is your auktority ? :).

just joking.
Yes, I also noticed that.... that its actually not propable the low
numbers would come. Its just difficult as a human to see that
intuitively (I would think we get also small ones). Yes I understand
mathematics...
0 new messages