Random Number Range Problem.
Hello all,
I am trying to create a random number between a range of certain numbers, lets say 1 -10. I am using this below but for some reason it gives me the same number most of the time.
Code:
int rand_func(int min, int max )
//this function takes 2 arguments: min and max
// and creates random numbers based on min an max and loads it into i
{
int i = 0;
i = rand();
printf("\t%d", i);
i = min + (max - min) * i / RAND_MAX; // min and max forumula
printf("\t%d\n",i);
return i;
}
I appreciate any response. Thank You!
You probaly got your answer
Quote:
Originally Posted by xamlit
Hello all,
I am trying to create a random number between a range of certain numbers, lets say 1 -10. I am using this below but for some reason it gives me the same number most of the time.
It does'nt give you the same number most of the time.
But max isn't behaving.
Code:
int rand_func(int min, int max )
{
int i = 0;
i = rand();
/* printf("%d : ", i); */
i = min + (max - min) * i / RAND_MAX;
/* printf("\t%d\n\n",i); */
return i;
}
void calc_avg(int min, int max, int cnt)
{
int range;
int i;
int res;
int arr[100];
range = max - min + 1;
if ( range > sizeof(arr)/sizeof(arr[0]) )
{
fprintf(stderr,"Enlarge arr to %d\n", range);
return;
}
memset(arr, 0, sizeof(arr));
for ( i = 0 ; i < cnt ; ++i )
{
res = rand_func(min, max);
++arr[ res - min ] ;
}
printf("Resultes with min = %d max = %d. numbers of repeats %d :\n"
,min, max, cnt);
printf("Best stat is %.4f%%\n", 1.0/range*100.0);
for ( i = 0 ; i < range ; ++i)
printf("%4d : %7d stat = %.4f%%\n",
i + min, arr[i],
(arr[i]/(double)cnt)*100.0);
}
int main(void)
{
calc_avg(6, 8, 9999999);
calc_avg(1, 10, 9999999);
calc_avg(12001, 12009, 9999999);
calc_avg(19001, 19015, 9999999);
getchar();
return(0);
}
Resultes with min = 6 max = 8. numbers of repeats 9999999 :
Best stat is 33.3333%
6 : 5001173 stat = 50.0117%
7 : 4998537 stat = 49.9854%
8 : 289 stat = 0.0029%
Resultes with min = 1 max = 10. numbers of repeats 9999999 :
Best stat is 10.0000%
1 : 1112069 stat = 11.1207%
2 : 1110040 stat = 11.1004%
3 : 1110804 stat = 11.1080%
4 : 1109341 stat = 11.0934%
5 : 1110923 stat = 11.1092%
6 : 1111714 stat = 11.1171%
7 : 1110934 stat = 11.1093%
8 : 1112303 stat = 11.1230%
9 : 1111575 stat = 11.1158%
10 : 296 stat = 0.0030%
Resultes with min = 12001 max = 12009. numbers of repeats 9999999 :
Best stat is 11.1111%
12001 : 1249934 stat = 12.4993%
12002 : 1248477 stat = 12.4848%
12003 : 1251114 stat = 12.5111%
12004 : 1247631 stat = 12.4763%
12005 : 1250520 stat = 12.5052%
12006 : 1249625 stat = 12.4963%
12007 : 1251098 stat = 12.5110%
12008 : 1251295 stat = 12.5130%
12009 : 305 stat = 0.0031%
Resultes with min = 19001 max = 19015. numbers of repeats 9999999 :
Best stat is 6.6667%
19001 : 712749 stat = 7.1275%
19002 : 712608 stat = 7.1261%
19003 : 714874 stat = 7.1487%
19004 : 714646 stat = 7.1465%
19005 : 715011 stat = 7.1501%
19006 : 714185 stat = 7.1419%
19007 : 715245 stat = 7.1525%
19008 : 714332 stat = 7.1433%
19009 : 714068 stat = 7.1407%
19010 : 713911 stat = 7.1391%
19011 : 714799 stat = 7.1480%
19012 : 714357 stat = 7.1436%
19013 : 714453 stat = 7.1445%
19014 : 714458 stat = 7.1446%
19015 : 303 stat = 0.0030%
Try to run the numbers on paper
Code:
Lets say for exapmle :
min = 6 and max = 8
Lets assume RAND_MAX=13
so we get
i = 6 + 2 * {0,13}/13;
for rand>= 0 && rand<=6 we get i = 6
for rand>= 7 && rand<=12 we get i = 7
for rand>= 13 && rand<=13 we get i = 8