Reputation: 1829
I have the following C code:
int rand_num=0;
int card_vnum=0;
int legendary[] = { 400, 401, 402, 407 }; //Legendary
int epic[] = { 403, 404, 408, 409, 410, 416 }; //Epic
int rare[] = { 405, 406, 414 }; //Rare
int uncommon[] = { 411, 412, 413, 415, 417, 418, 419, 420, 421, 422, 423, 424 }; //Uncommon
int common[] = { 425, 426 }; //Common
int arr_size=0;
int picked_arr=0;
rand_num = number_range(1,1000);
if ( rand_num > 0 && rand_num <= 2 )
{
arr_size = sizeof(legendary)/sizeof(legendary[0]);
picked_arr = number_range(0, arr_size);
card_vnum = legendary[picked_arr];
debugmsg("Legendary card");
}
else if ( rand_num > 2 && rand_num <= 50 )
{
arr_size = sizeof(epic)/sizeof(epic[0]);
picked_arr = number_range(0, arr_size);
card_vnum = epic[picked_arr];
debugmsg("Epic card");
}
else if ( rand_num > 50 && rand_num <= 200 )
{
arr_size = sizeof(rare)/sizeof(rare[0]);
picked_arr = number_range(0, arr_size);
card_vnum = rare[picked_arr]; //memory issue here? vnum can end up as 403 (Epic)
debugmsg("Rare card");
}
else if ( rand_num > 200 && rand_num <= 450 )
{
arr_size = sizeof(uncommon)/sizeof(uncommon[0]);
picked_arr = number_range(0, arr_size);
card_vnum = uncommon[picked_arr];
debugmsg("Uncommon card");
}
else if ( rand_num > 450 && rand_num <= 1000 )
{
arr_size = sizeof(common)/sizeof(common[0]);
picked_arr = number_range(0, arr_size);
card_vnum = common[picked_arr]; //memory issue here? vnum can end up as 411 (Uncommon)
debugmsg("Common card");
}
debugmsg("Card: %d, chance: %d", card_vnum, rand_num);
Example results (should have been a Common card):
Debug: Common card
Debug: Card: 411, chance: 930
When you open your hand, you're holding... (Uncommon) card
number_range (from the Smaug MUD codebase):
int number_range( int from, int to )
{
if ( (to-from) < 1 )
return from;
return ((number_mm() % (to-from+1)) + from);
}
int number_mm( void )
{
int *piState;
int iState1;
int iState2;
int iRand;
piState = &rgiState[2];
iState1 = piState[-2];
iState2 = piState[-1];
iRand = (piState[iState1] + piState[iState2])
& ((1 << 30) - 1);
piState[iState1] = iRand;
if ( ++iState1 == 55 )
iState1 = 0;
if ( ++iState2 == 55 )
iState2 = 0;
piState[-2] = iState1;
piState[-1] = iState2;
return iRand >> 6;
}
I'm having issues where if the random number (that's what number_range
does) falls into an ifcheck, it sometimes grabs from the wrong array. I assume this is a memory issue where I may be sizing the arrays wrong.
I have example issues in the comments above. The goal is once it falls into an ifcheck, it grabs a random number from the specific array.
The issue is sometimes it grabs a random number from the wrong array. Any thoughts?
Upvotes: 2
Views: 81
Reputation: 206577
The line
return ((number_mm() % (to-from+1)) + from);
can return the wrong index for an array.
Let's say from = 0
and to = 5
. Then, to-from+1 = 6
. The range of values that can evaluate to is [0-5]
. However, you want that range to be [0-4].
Changing that line to
return ((number_mm() % (to-from)) + from);
will fix that problem.
The other alternative is to change how the function gets called. Instead of using
picked_arr = number_range(0, arr_size);
you can use
picked_arr = number_range(0, arr_size-1);
Upvotes: 1