は、あなたが名前の配列を保存したと仮定すると、あなたの問題に対する可能な解決策だだけの位置がランダムなものになりますので、それは数回シャッフルその後、位置の配列を作成し、最後に(3つのポジションを選びます例えば、最初の3つのもの):この方法では
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 6
#define COL 20
#define RND_NAMES 3
void shuffle(int *array, int n, int num_shuffles) {
srand((unsigned)time(NULL));
for (int j = 0; j < num_shuffles; j++) {
for (int i = 0; i < n - 1; i++) {
size_t j = i + rand()/(RAND_MAX/(n - i) + 1);
int t = array[j];
array[j] = array[i];
array[i] = t;
}
}
}
int main() {
int i;
char name[ROWS][COL] = {"name1", "name2", "name3",
"name4", "name5", "name6"};
int positions[ROWS] = {0, 1, 2, 3, 4, 5};
shuffle(positions, ROWS, 100);
printf("%s\n", name[positions[0]]);
printf("%s\n", name[positions[1]]);
printf("%s\n", name[positions[2]]);
return 0;
}
、あなたは3ランダムな非繰り返し名前を拾うことが保証しています。
出典
2017-05-25 09:49:32
BPL
[0-5]の範囲の乱数を生成し、それを使用して名前を選択します。 –
「1単語」と「1単語」の違いは何ですか? –
@ScottHunterねえ、あなたは「ワンタイムジョブ」と「フルタイムジョブ」についてそれを聞かないでしょうか? :) –