Verify distribution uniformity/Naive: Difference between revisions

→‎{{header|C}}: it was a banal *value++ instead of ++*value or similar ;) however, rewritten to avoid the use of two hashes
(→‎{{header|C}}: it was a banal *value++ instead of ++*value or similar ;) however, rewritten to avoid the use of two hashes)
Line 19:
 
=={{header|C}}==
{{incorrect|C|I suppose there's some error in inserting / getting the values; it must be checked in relationship to Dice5/Dice7, since it gives always skewed, which is odd...}}
 
{{libheader|Judy}}
<lang c>#include <stdio.h>
Line 33 ⟶ 31:
Pvoid_t h = (Pvoid_t) NULL;
PWord_t value;
PWord_t element;
Pvoid_t hi = (Pvoid_t)NULL;
 
Word_t i;
Word_t t; // temp for lazyness in using JudyLFreeArray directly
int i, j, h_length;
 
// populate hashes; the hi hash is needed to
for(i=0, j=0; i < n; i++) {
// be able to iterate later on key(rn) - value pairs; indeed
// we could use JLF (first index in array) and then JLN (next index)
for(i=0, j=0; i < n; i++) {
int rn = dist();
JLG(value, h, rn);
if ( value == NULL ) {
JLI(value, hi, j);
*value = rn;
j++;
}
JLI(value, h, rn);
*value++*value;
}
 
JLC(h_length, h, 0, -1);
h_length = j; // we could use JLC too to count how many idx are
 
// into the h array.
double target = 1.0 * n / (double)h_length;
 
i = j++0;
for(i=0; i < h_length; i++) {
JLGJLF(valueelement, hih, i);
while(element != NULL) {
int k = *value;
if ( abs(v*element - target) > 0.01*n*D ) {
JLG(value, h, k);
int v = *value; // now we have couple key-value
if ( abs(v - target) > 0.01*n*D ) {
fprintf(stderr, "distribution potentially skewed for '%d': expected '%d', got '%d'\n",
ki, (int)target, v*element);
JLFAJudyLFreeArray(t, &h); JLFA(t, hiPJE0);
return false; // bad distr.
}
JLGJLN(valueelement, h, rni);
}
JLFA(t, h); JLFA(t, hi);
 
JudyLFreeArray(&h, PJE0);
return true; // distr. ok
}
 
 
int frand()
{
return rand() % 10;
}
 
int main()
{
distcheck(frandrand, 1000000, 1);
return 0;
}</lang>