카드 섞는 알고리즘
여러가지 알고리즘이 있으나 inside-out 에 대해서 알아 보도록 하자.
#include <stdlib.h>
#include <time.h>
#define MAX_CARD_CNT 54
void shuffling_insideout()
{
int a[MAX_CARD_CNT];
int source[MAX_CARD_CNT];
int index = 0;
for ( int i = 0 ; i < MAX_CARD_CNT ; i++ )
source[i] = i;
a[0] = source[0];
for ( int j = 1; j < MAX_CARD_CNT ; j++ )
{
srand(time(NULL));
index = rand() % (j+1);
a[j] = a[index];
a[index] = source[j];
}
}
하단의 알고리즘을 기반으로 구현한 것이 므로 오류가 있을수 있습니다. 참고만 하세요.
To initialize an array a of n elements to a randomly shuffled copy of source, both 0-based: a[0] ← source[0] for i from 1 to n − 1 do j ← random integer with 0 ≤ j ≤ i a[i] ← a[j] a[j] ← source[i]
댓글