"David B. Held" <dh***@codelogicconsulting.com> wrote in message
news:bn**********@news.astound.net...
"lallous" <la*****@lgwm.org> wrote in message
news:bn************@ID-161723.news.uni-berlin.de...
This code works fine when 'size' is less than 32768 however
when size is bigger this function never returns.
Can't find out why?!
It would seem that the most likely problem is here:
[...]
double f = ((double) rand()) / RAND_MAX;
n = (unsigned long)(f * sizef);
if (map[n])
continue;
[...]
Perhaps your RNG doesn't give sufficient resolution after
floating-point conversions to cover your domain. One way
to test this is to write a loop which tries to obtain each
number from 0 to sizef, and maybe displays the number of
attempts to get it. That way, you get a better idea of the
coverage your RNG is providing.
Dave
Wrote some function:
void SearchForZeroAndSize(unsigned long size)
{
double sizef = (double)(size);
unsigned long n;
srand((unsigned)time(NULL));
unsigned long ntries(0);
bool nozero = true, nosize = true;
while (nozero || nosize)
{
double f = ((double) rand()) / RAND_MAX;
f = f * sizef;
n = (unsigned long)(f);
if (n==0 && nozero)
{
printf("found zero after %ld tries\n", ntries);
nozero = false;
}
else if (n==size && nosize)
{
printf("found 'size' after %ld tries\n", ntries);
nosize = false;
}
ntries++;
}
}
Output:
found 'size' after 9902 tries
found zero after 44910 tries
Here is some more info:
unsigned long i, biggest = 0;
for (i=0; i < size; i++)
{
for (;;)
{
bool flag = false;
double f = ((double) rand()) / RAND_MAX;
f = f * sizef;
n = (unsigned long)(f);
if (n > biggest)
{
biggest = n;
if (biggest == 99999)
n = biggest; // useless code, but just to put a breakpoint
printf("biggest so far: %ld\n", biggest);
}
if (flag) // when the function hangs, put a BP here and adjust flag to
TRUE
{
unsigned long filled(0), unfilled(0);
for (unsigned long j=0;j<size;j++)
{
if (!map[j])
{
unfilled++;
//printf("%d is not filled!\n", j);
}
else
filled++;
if (j % 1000 == 0)
{
//printf("filled: %ld unfilled:%ld\n", filled, unfilled);
}
}
printf("filled: %ld unfilled:%ld\n", filled, unfilled);
}
if (map[n])
continue;
data[i] = n;
map[n] = 1;
break;
}
}
The output goes:
biggest so far: 97978
biggest so far: 98790
biggest so far: 99510
biggest so far: 99718
biggest so far: 99916
biggest so far: 99995
biggest so far: 99999
filled: 32768 unfilled:67232
This asserts that I am getting random numbers above 32768...
p.s: I am using VC6++
--
Elias
http://lgwm.org/