如何从10个字符串的数组中打印5个唯一的字符串

时间:2022-09-06 13:13:28

I am writing a code which will print 5 unique and random strings from an array of 10 strings. But my code doesn't print it uniquely, there are always some repetitions. Here is my code, can anyone suggest how to make it print unique?

我正在编写一个代码,它将从10个字符串的数组中打印5个唯一且随机的字符串。但我的代码并不是唯一打印的,总会有一些重复。这是我的代码,任何人都可以建议如何使其打印独特?

#include<stdio.h>
#include<stdlib.h>
#include<math.h>




void main(void){
    char arr[10][10]={"abc","def","ghi","klm","nop","qrs","tuv","wxy","zab","cde"};
    int i=0,j=0;

    srand(time(0));

    for(i=0;i<5;i++){

    j=rand()%10;
    printf("%d\n",j);

    }
}

4 个解决方案

#1


2  

Just remember idices that have already been printed out:

记住已经打印过的错误:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    char arr[10][10] = {"abc","def","ghi","klm","nop","qrs","tuv","wxy","zab","cde"};
    int i = 0, j = 0;
    int done[10] = { 0 };

    srand(time(0));

    for (i = 0; i < 5; ++i)
    {
        do
        {
            j = rand()%10;
        }
        while (done[j] == 1);
        done[j] = 1;
        printf("%s\n", arr[j]);
    }

    return 0;
}

#2


2  

Your rand() function may evaluate to the same string index multiple times. There are different ways to resolve that. Most of them will be a take off on bit vector approach. You can create a Boolean array and initialize it to false. Whenever a number is printed, you can change the element in the Boolean array for that index to true. If an element is selected, you can check in the Boolean array if it has already been printed. If yes, you can regenerate the random number, or move to the next unprinted element, making sure that the corresponding Boolean entry is changed to true when you print that element.

您的rand()函数可能会多次计算到相同的字符串索引。有不同的方法来解决这个问题。他们中的大多数将是位向量方法的起飞。您可以创建一个布尔数组并将其初始化为false。无论何时打印数字,您都可以将该索引的布尔数组中的元素更改为true。如果选择了某个元素,则可以检查布尔数组中是否已打印。如果是,您可以重新生成随机数,或移动到下一个未打印的元素,确保在打印该元素时相应的布尔条目更改为true。

#3


2  

Your random function gives the repetition, e.g. your dice throw '3' two time out of 4 throws.

你的随机函数给出了重复,例如你的骰子在4次投掷中两次投掷'3'。

What you could do to make it simple, create an array [0..9], and let the random function swap a[i] with a[j].

你可以做些什么来简化它,创建一个数组[0..9],然后让随机函数用[j]交换a [i]。

Then you get a random permutation. Take the first 5 elements of the permutated array.

然后你得到一个随机的排列。获取置换数组的前5个元素。

#4


1  

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <time.h>

int main(){
    char arr[10][10]={"abc","def","ghi","klm","nop","qrs","tuv","wxy","zab","cde"};
    int found[10] = { 0 };
    int i = 0,j = 0;
    srand(time(NULL));
    for(i = 0; i < 5; i++){
        j = rand() % 10;
        while(1){               
            if(!found[j]) break;
            j = rand() % 10;
        }
        found[j] = 1;
        printf("%s\n", arr[j]);
    }
    return 0;
}

#1


2  

Just remember idices that have already been printed out:

记住已经打印过的错误:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    char arr[10][10] = {"abc","def","ghi","klm","nop","qrs","tuv","wxy","zab","cde"};
    int i = 0, j = 0;
    int done[10] = { 0 };

    srand(time(0));

    for (i = 0; i < 5; ++i)
    {
        do
        {
            j = rand()%10;
        }
        while (done[j] == 1);
        done[j] = 1;
        printf("%s\n", arr[j]);
    }

    return 0;
}

#2


2  

Your rand() function may evaluate to the same string index multiple times. There are different ways to resolve that. Most of them will be a take off on bit vector approach. You can create a Boolean array and initialize it to false. Whenever a number is printed, you can change the element in the Boolean array for that index to true. If an element is selected, you can check in the Boolean array if it has already been printed. If yes, you can regenerate the random number, or move to the next unprinted element, making sure that the corresponding Boolean entry is changed to true when you print that element.

您的rand()函数可能会多次计算到相同的字符串索引。有不同的方法来解决这个问题。他们中的大多数将是位向量方法的起飞。您可以创建一个布尔数组并将其初始化为false。无论何时打印数字,您都可以将该索引的布尔数组中的元素更改为true。如果选择了某个元素,则可以检查布尔数组中是否已打印。如果是,您可以重新生成随机数,或移动到下一个未打印的元素,确保在打印该元素时相应的布尔条目更改为true。

#3


2  

Your random function gives the repetition, e.g. your dice throw '3' two time out of 4 throws.

你的随机函数给出了重复,例如你的骰子在4次投掷中两次投掷'3'。

What you could do to make it simple, create an array [0..9], and let the random function swap a[i] with a[j].

你可以做些什么来简化它,创建一个数组[0..9],然后让随机函数用[j]交换a [i]。

Then you get a random permutation. Take the first 5 elements of the permutated array.

然后你得到一个随机的排列。获取置换数组的前5个元素。

#4


1  

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include <time.h>

int main(){
    char arr[10][10]={"abc","def","ghi","klm","nop","qrs","tuv","wxy","zab","cde"};
    int found[10] = { 0 };
    int i = 0,j = 0;
    srand(time(NULL));
    for(i = 0; i < 5; i++){
        j = rand() % 10;
        while(1){               
            if(!found[j]) break;
            j = rand() % 10;
        }
        found[j] = 1;
        printf("%s\n", arr[j]);
    }
    return 0;
}