#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(void){
int num[] = {1, 1, 2, 2, 3, 3}; // Given array which has to be shuffled
int swap;
int i, j;
srand(time(NULL));
for(i = 1; i < 6; i++){ // Start of Fisher Yates Shuffle
j = rand() % (i + 1);
if(j != i){
swap = num[j];
num[j] = num[i];
num[i] = swap;
}
} // End of FISHER-YATES
// Printing the array
printf("\nThe array is [ ");
for(i = 0; i < 6; i++){
printf("%d ", num[i]);
}
printf("]");
}
[Edited]: {Since a lot of you requested further clarification in the question, I am editing the question to make things clearer. Changes Made:
Use of "equal" instead of similar in line-3(Paragraph 1)
By spacing of 2, I mean that a number can appear again only after 2 other different numbers have appeared after it. (For example, [3, 1, 2, 3, 1, 2] is a correct output while [3, 1, 3, 2, 1, 2] is incorrect) And the spacing has to be "at least 2".
Use of word "different/ unequal" in line-4(Paragraph 2) }
I tried placing counters to check the 2 indexes ahead of the current value of the array and choose to shuffle or not shuffle based on the value at that index being "equal" or not "equal" to the number being shuffled.
What I am trying to do is make a general program that can similarly take in 'n' "distinct" values and shuffle with a constraint or spacing of maximum 'n-1' (Because for example, to have a spacing of 3 between same elements you need minimum 3 other "different/ unequal" elements in the array, therefore there need to be at least 4 distinct elements for a spacing of 3 between equal numbers).
Hope this helps you to understand what I'm trying to say.