在C中使用数组的Mem问题

时间:2014-06-15 17:27:49

标签: c arrays

我有以下C代码:

    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);

示例结果(应该是普通卡):

Debug: Common card
Debug: Card: 411, chance: 930
When you open your hand, you're holding... (Uncommon) card

number_range(来自Smaug MUD代码库):

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;
}

我遇到的问题是,如果随机数(number_range做什么)属于ifcheck,它有时会从错误的数组中获取。我认为这是一个内存问题,我可能会错误地调整数组大小。

我在上面的评论中有例子问题。目标是一旦陷入ifcheck,它就会从特定数组中获取一个随机数。

问题有时会从错误的数组中抓取一个随机数。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

该行

return ((number_mm() % (to-from+1)) + from);

可以为数组返回错误的索引。

让我们说from = 0to = 5。然后,to-from+1 = 6。可以评估的值范围是[0-5]。但是,您希望该范围为[0-4]。

将该行更改为

return ((number_mm() % (to-from)) + from);

将解决这个问题。

另一种选择是改变函数的调用方式。而不是使用

    picked_arr = number_range(0, arr_size);

你可以使用

    picked_arr = number_range(0, arr_size-1);