I'm trying to shuffle an array by following order:
- user inputs the size of array
- user inputs number of times to shuffle
- shuffling function should run by selecting two random number(index) and switching the value of indices. For example, if I made my array arr[10], the fuction should pick two random numbers in range 0~9 and switch positions.
- If the user typed 2 in step2, the function should repeat picking two random indices 2 times.
I tried, but something seems wrong(the two numbers are repeatedly identical). How can I get real random numbers?
void shuffle(int arr[], int size) {
srand(time(NULL));
int temp, rand1, rand2;
rand1 = rand() % size;
rand2 = rand() % size;
temp = arr[rand1];
arr[rand1] = arr[rand2];
arr[rand2] = temp;
}
void total_shuffle(int arr[], int size, int num) {
for (int i = 0; i < num; i++)
shuffle(arr, size);
}