D
DFS
I've been reading up on rand() for the first time, and found this article:
http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
On web, saw lots of recommendations to use modulo
to limit the result set from 0 to N.
r = rand() % N
A=1428954
B=1427121
C=1430690
D=1428133
E=1429276
F=1426874
G=1428952
Total=10000000
the article above gives another way
r = rand() / ((RAND_MAX / N) + 1)
A=1426636
B=1430108
C=1427855
D=1428752
E=1429170
F=1428667
G=1428812
Total=10000000
To my uninformed eye, both ways produce well-distributed random results
within a 10 million size test loop.
(intentionally clunky test) code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
char randomLetter(int runType) {
int i, r;
int countLetters = 7;
int Acounter = 0, Bcounter = 0, Ccounter = 0, Dcounter = 0;
int Ecounter = 0, Fcounter = 0, Gcounter = 0;
srand(time(NULL));
for (i=1; i<=10000000;i++)
{
if (runType == 1) {r = rand() % (countLetters);}
if (runType == 2) {r = rand() / ((RAND_MAX / countLetters) + 1);}
if (r == 0) Acounter++;
if (r == 1) Bcounter++;
if (r == 2) Ccounter++;
if (r == 3) Dcounter++;
if (r == 4) Ecounter++;
if (r == 5) Fcounter++;
if (r == 6) Gcounter++;
}
if (runType == 1) printf("r = rand() %% %d\n", countLetters);
if (runType == 2) printf("r = rand() / ((RAND_MAX / %d) + 1)\n",
countLetters);
printf("A=%d\n",Acounter);
printf("B=%d\n",Bcounter);
printf("C=%d\n",Ccounter);
printf("D=%d\n",Dcounter);
printf("E=%d\n",Ecounter);
printf("F=%d\n",Fcounter);
printf("G=%d\n",Gcounter);
printf("Total=%d\n",Acounter+Bcounter+Ccounter+Dcounter+Ecounter+Fcounter+Gcounter);
return 0;
}
int main(void) {
randomLetter(1);
randomLetter(2);
return 0;
}
http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
On web, saw lots of recommendations to use modulo
to limit the result set from 0 to N.
r = rand() % N
A=1428954
B=1427121
C=1430690
D=1428133
E=1429276
F=1426874
G=1428952
Total=10000000
the article above gives another way
r = rand() / ((RAND_MAX / N) + 1)
A=1426636
B=1430108
C=1427855
D=1428752
E=1429170
F=1428667
G=1428812
Total=10000000
To my uninformed eye, both ways produce well-distributed random results
within a 10 million size test loop.
(intentionally clunky test) code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
char randomLetter(int runType) {
int i, r;
int countLetters = 7;
int Acounter = 0, Bcounter = 0, Ccounter = 0, Dcounter = 0;
int Ecounter = 0, Fcounter = 0, Gcounter = 0;
srand(time(NULL));
for (i=1; i<=10000000;i++)
{
if (runType == 1) {r = rand() % (countLetters);}
if (runType == 2) {r = rand() / ((RAND_MAX / countLetters) + 1);}
if (r == 0) Acounter++;
if (r == 1) Bcounter++;
if (r == 2) Ccounter++;
if (r == 3) Dcounter++;
if (r == 4) Ecounter++;
if (r == 5) Fcounter++;
if (r == 6) Gcounter++;
}
if (runType == 1) printf("r = rand() %% %d\n", countLetters);
if (runType == 2) printf("r = rand() / ((RAND_MAX / %d) + 1)\n",
countLetters);
printf("A=%d\n",Acounter);
printf("B=%d\n",Bcounter);
printf("C=%d\n",Ccounter);
printf("D=%d\n",Dcounter);
printf("E=%d\n",Ecounter);
printf("F=%d\n",Fcounter);
printf("G=%d\n",Gcounter);
printf("Total=%d\n",Acounter+Bcounter+Ccounter+Dcounter+Ecounter+Fcounter+Gcounter);
return 0;
}
int main(void) {
randomLetter(1);
randomLetter(2);
return 0;
}