c++ generation of random numbers without duplication

Asked 2 years ago, Updated 2 years ago, 41 views

srand((unsigned)time(NULL); // Generate a different random number each time you run a program
int found1, found2;
string fruit_1[8], fruit_2[8];
string fruit[8] = { "Apple", "Apple", "Grape", "Grape", "Melon", "Tangerine" };
for (int i = 0; i < 8; i++) {
    while (1) {
        fruit_1[i] = fruit[rand() % 8];
        found1 = 0;
        for (int j = 0; j < i; j++) {
            if (fruit_1[j] == fruit_1[i])
                found1 = 1;
        }
        If (found1 == 0)break; // no value exists, end random number regeneration
    }

    while (1) {
        fruit_2[i] = fruit[rand() % 8];
        found2 = 0;
        for (int j = 0; j < i; j++) {
            if (fruit_2[j] == fruit_2[i])
                found2 = 1;
        }
        If (found2 == 0)break; // no value exists, then random number regeneration ends
    }
    p[i].set_fruit1(fruit_1[i]);
    p[i].set_fruit2(fruit_2[i]);
    cout << p[i].get_name() <<"Fruit1-" <<p[i].get_fruit1() <<"Fruit2-" <<p[i].get_fruit2() << endl;
}

It's a code that randomly allocates two fruits per player, but the following sentence of this code is not executed. I'm not sure if I'm a beginner Help me.

c++

2022-09-22 13:59

1 Answers

    for (int i = 0; i < 8; i++) {
        while (1) {
            fruit_1[i] = fruit[rand() % 8];
            found1 = 0;
            for (int j = 0; j < i; j++) {
                if (fruit_1[j] == fruit_1[i])
                    found1 = 1;
            }
            if (found1 == 0){
                Break; If no value such as //, end random number regeneration
            }

In the

One. fruit_1[i] = fruit[rand()%8]; After getting a random fruit name, 2-1. If fruit_1 has the same fruit in the while group, pick the fruit again. 2-2. If fruit_1 does not have the same fruit in the while group, put the fruit in the array. 3. Repeat the above process 8 times (inti = 0; i < 8; i++).

The problem is that string fruit [8] = { "Apple", "Apple", "Grape", "Grape", "Grape", "Melon", "Melon", "Tangerine" }; contains two elements of the same value.

So when I = 4, fruit_1 contains all of the words "apple," "grape," "male," and "tangerine After this, fruit_1[i] = fruit[rand()%8]; is forced to pull the value contained in fruit_1. Therefore, the new value cannot be drawn, and the found1 == 0 cannot be caught, resulting in an infinite loop.

By any chance fruit_1 = {"Apple", "Grape", "Apple", "Grape", "Melon", "Tangerine", "Melon"} If you want to put each fruit in random order twice in fruit_1 like this,

        while (1) {
            fruit_1[i] = fruit[rand() % 8];
            found1 = 0;
            for (int j = 0; j < i; j++) {
                if (fruit_1[j] == fruit_1[i])
                    found1 ++;
            }
            if (found1 <= 1){
                Break; If no value such as //, end random number regeneration
            }
        }

Change the code like this.


2022-09-22 13:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.