从一个数组到另一个数组的随机数

时间:2018-04-24 02:35:23

标签: c arrays random

我正在为嵌入式电路板编程以点亮4个LED(8个)。 我想随机化那些点亮的,并重复这个过程。 基本上,通过从一组设定数字(LED位值)复制值,使它们随机,并将它们插入另一个数组。第二个阵列需要唯一的值(即相同的4个LED,但是采用独特的随机模式)。我需要复制这个x次,因此主要是while循环。

目前,我在第二个数组中重复了数字。我相信这是因为嵌套的for循环没有正确破坏。我试过break,使for循环计数器(i / j)达到最大值(这会导致无限循环),甚至转到。

到目前为止无济于事。任何纠正或建议,以更好的方式来解决这个问题!

int main() {
    srand(time(NULL));                  //Set seed random number
    int ledORIGINAL[4] = { 2,4,6,8 };   //Array of defined numbers to use
    int led[4] = { 0,0,0,0 };           //Empty array (set to 0's)
    int rIndex=0, ledIndex=0, loop=0;   //Index variables
    bool originalNum = false;           //Boolean flag to find original number

    while (loop < 2) {                  //Set how many random arrays you need

        while (ledIndex < 4) {          //Repeat util all 4 array slots filled
            rIndex = rand() % 4;        //Get a random index number
            if (led[ledIndex] == 0) {               //If the array slot is empty
                for (int i = 0; i < 4; i++) {       //Nested for loops to check number is not already in array
                    for(int j=0; j<4; j++){
                        if (led[i] != ledORIGINAL[j]) {
                            originalNum = true;
                        }
                        else {
                            originalNum = false;        //Boolean flag set to false, no need to search further
                            //i= 4;                     //Set i to 4 to break outer loop
                            //j = 4;                        //Set j to 4 to break inner loop
                            break;
                            //goto PLACE;

                        }
                    }
                }
                //PLACE:
                if (originalNum) {
                    led[ledIndex] = ledORIGINAL[rIndex];
                    ledIndex++;
                }

            }
        }

        for (int i = 0; i < 4; i++) {
            std::cout << led[i];
            led[i] = 0;
        }
        ledIndex = 0;
        loop++;
    }
}

1 个答案:

答案 0 :(得分:0)

强烈建议用以下内容替换发布的代码:

struct fourLeds = { int led[4] };
const struct fourLeds ledTable[] =
{
    {8, 7, 6, 5},
    {8, 7, 6, 4},
    {8, 7, 6, 3},
    {8, 7, 6, 2},
    {8, 7, 6, 1},

    {8, 6, 5, 4},
    {8, 6, 5, 3},
    {8, 6, 5, 2},
    {8, 6, 5, 1},

    {8, 6, 4, 3},
    {8, 6, 4, 2},
    {8, 6, 4, 1},

    {8, 6, 3, 2},
    {8, 6, 3, 1},

    {8, 6, 2, 1},

    {8, 5, 4, 3},
    {8, 5, 4, 2},
    {8, 5, 4, 1],

    {8, 4, 3, 2},
    {8, 4, 3, 1},

    {8, 3, 2, 1}, 

    {7, 6, 5, 4},
    {7, 6, 5, 3},
    {7, 6, 5, 2},
    {7, 6, 5, 1},
    ....
    {7, 5, 4, 3},
    {7, 5, 4, 2},
    {7, 5, 4, 1},
    ....
    {7, 5, 2, 1},

    {7, 4, 3, 2},
    {7. 4, 3, 1},

    {7, 4, 2, 1},

    {7, 3, 2, 1},

    {6, 5. 4, 3},
    {6, 5, 4, 2},
    {6, 5, 4, 1},

    {6, 4, 3, 2},
    {6, 4, 3, 1},

    {6, 3, 2, 1},

    {5, 4, 3, 2},
    {5, 4, 3, 1},

    {5, 4, 2, 1},

    {5, 3, 2, 1},

    {4, 3, 2, 1},
};

int selection = rand() %( sizeof(ledTable) / sizeof(ledTable[0]) );
then output  `ledTable[ selection ]`