#include "test_helpers.h"
#include "ulong_extras.h"
TEST_FUNCTION_START(n_urandint, state)
{
ulong limit, rand_num;
slong deviation;
int i, j, result;
int * count;
int * count_in_subrange;
slong test_multiplier;
test_multiplier = FLINT_MAX(1, flint_test_multiplier());
count = flint_malloc(sizeof(int) * 1000);
for (limit = 1; limit <= 1000; limit+=10)
{
for (i = 0; i < limit; i++)
{
count[i] = 0;
}
for (i = 0; i < 1000 * test_multiplier; i++)
{
rand_num = n_urandint(state, limit);
count[rand_num]++;
}
result = 1;
for (i = 0; i < limit; i++)
{
deviation = count[i] - (1000 * (int) test_multiplier)/limit;
if (deviation >= WORD(100) * test_multiplier ||
deviation <= WORD(-100) * test_multiplier)
{
result = 0;
break;
}
}
if (!result)
TEST_FUNCTION_FAIL("limit = %wu, deviation = %wd\n", limit, deviation);
}
flint_free(count);
count_in_subrange = flint_malloc(sizeof(int) * 4);
for (i = 0; i < 1000; i+=10)
{
for (j = 0; j < 4; j++)
{
count_in_subrange[j] = 0;
}
limit = UWORD_MAX/(i + 2)*(i + 1);
for (j = 0; j < 1000 * test_multiplier; j++)
{
rand_num = n_urandint(state, limit);
if (rand_num >= 3*(limit >> 2))
{
count_in_subrange[3]++;
}
else if (rand_num >= 2*(limit >> 2))
{
count_in_subrange[2]++;
}
else if (rand_num >= (limit >> 2))
{
count_in_subrange[1]++;
}
else
{
count_in_subrange[0]++;
}
}
result = 1;
for (j = 0; j < 4; j++)
{
deviation = count_in_subrange[j] - ((1000 * (int) test_multiplier) >> 2);
if (deviation >= WORD(100) * test_multiplier ||
deviation <= WORD(-100) * test_multiplier)
{
result = 0;
break;
}
}
if (!result)
TEST_FUNCTION_FAIL("limit = %wu, deviation = %wd\n", limit, deviation);
}
flint_free(count_in_subrange);
TEST_FUNCTION_END(state);
}