>> In Nominal Animal's post he says "Again, none of this aliasing occurs if you just use min + (int)((double)interval * prng())."
I'm assuming that's a brain fart.
If you look at the end of post #10, the same formula is presented with:
>> Yes, this way will have some aliasing, too ...
This is the "+1" that I was thinking of:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int rand26_A()
{
return (int)((double)rand() / RAND_MAX * 26.0);
}
int rand26_B()
{
return (int)((double)rand() / (RAND_MAX + 1) * 26.0);
// ^
// here
}
void test(int (*rand26)())
{
int table[26] = {0};
int n;
for (n = 0; n < 100000; ++n)
{
int i = rand26();
if (i < 0 || i > 25)
printf("Uh Oh\n");
else
++table[i];
}
for (n = 0; n < 26; ++n)
printf("%d\n", table[n]);
printf("\n\n");
}
int main()
{
printf("rand26_A:\n");
srand(26);
test(&rand26_A);
printf("rand26_B:\n");
srand(26);
test(&rand26_B);
return 0;
}
Code:
rand26_A:
Uh Oh
Uh Oh
Uh Oh
Uh Oh
Uh Oh
Uh Oh
Uh Oh
3860
3788
3914
3830
3958
3893
3918
3825
3747
3788
3845
3830
3830
3892
3886
3827
3851
3731
3885
3930
3877
3761
3730
3862
3808
3927
rand26_B:
3860
3788
3914
3830
3958
3893
3919
3824
3747
3788
3847
3828
3830
3895
3883
3827
3854
3735
3878
3932
3878
3763
3727
3862
3808
3932
>> Until I figure out a way to get around rand()'s distribution problem
The best you can do to preserve rand's distribution is to use "exclusion method" in post #13 (which is from "Accelerated C++").
gg