現在、下記の様に実装しているのですが無駄なのでしょうか?
int a = rand%10;
int b = rand%10;
int c = rand%10;
while(1){
if(a!=b)break;
else b = rand%10;
}
while(1){
if(a!=c && b!=c) break;
else c = rand%10;
}int a = rand%10;
int b = rand%10;
int c = rand%10;
while(1){
if(a!=b)break;
else b = rand%10;
}
while(1){
if(a!=c && b!=c) break;
else c = rand%10;
}#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void swap( int *a, int *b ){
int t = *a;
*a = *b;
*b = t;
}
int main(){
int i=0,r,arr[10]={0,1,2,3,4,5,6,7,8,9};
srand((unsigned)time(NULL));
while( i < 10 ){
r = rand() % 10;
if( r == i ){ //同じもの交換してもショウガナイ
continue; //のでもう一度
}
swap( &arr, &arr[[/url] );
i++;
}
for( i=0; i<10; i++ ){
printf("%d ", arr);
}
return 0;
}
実行結果
2 9 7 4 8 6 3 5 0 1int main(){
int i,arr[10]={0,1,2,3,4,5,6,7,8,9};
srand((unsigned)time(NULL));
for( i=0; i<10; i++)
swap( &arr, &arr[(((rand() % 9) + (i + 1)) % 10)] );
return 0;
} #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
int main(void)
{
int i = 0, j = 0, temp = 0, array[N];
// 乱数の種を設定する
srand((unsigned)time(NULL));
// 配列の初期化
for (i = 0; i < N; ++i)
{
array = i;
}
// Fisher-Yatesによるシャッフル処理
for (i = N - 1; i > 1; --i)
{
j = rand() % (i + 1);
temp = array[j];
array[j] = array;
array = temp;
}
// シャッフルした配列の表示
for (i = 0; i < N; ++i)
{
printf("%d\n", array);
}
return 0;
}