15.01.2017, 15:58
I going crazy.
Why I get random values like -164984 when I've [-1000;1000] ?
EDIT:
10'000 tests, no errors:
I test like 5 times on my script, I get 2 erroneous values.
Why I get random values like -164984 when I've [-1000;1000] ?
PHP код:
randomEx(min, max)
{
static
bool:negative;
if(min < 0 || max < 0)
negative = true;
if(min < 0)
min--;
else
min++;
if(max < 0)
max--;
else
max++;
if(!negative)
{
return random(max - min) + min;
}
else
{
if(random(2) == 1)
{
if(min == 0)
return min;
return -random(-min);
}
else
{
if(max == 0)
return max;
return random(max);
}
}
}
10'000 tests, no errors:
PHP код:
main()
{
const
a = -1000,
b = -a;
new c;
for(new i; i < 10000; i++)
{
c = randomEx(a, b);
//printf("Test [%i] = %i", i, c);
if(c < a || c > b)
printf("[Error]: test %i : %i", i, c);
}
}