#Shuffling algorithm

5 messages · Page 1 of 1 (latest)

supple crescentBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#
for (i = 0; i < totalsongs * 2; i++) {
  int j = rand() % (i + 1);
  char swap[MAX_LENGTH];

  while (!check(j, doubled)) {
    j = rand() % (i + 1);
  }

  // Update the shuffled playlist
  strcpy(swap, doubled[i]);
  strcpy(doubled[i], doubled[j]);
  strcpy(doubled[j], swap);
}

printf("Shuffled playlist:\n");
for (int i = 0; i < totalsongs * 2; i++) {
  printf("%s", doubled[i]);
}
Amo
sly sonnet
#

shuffling algorithm

supple crescentBOT
#
bool check(int j, char doubled[24][80]) {
  int max = j - 5;
  if (max < 0) {
    max = 0;
  }
  for (; max < j; max++) {
    if (strcmp(doubled[j], doubled[max]) == 0) {
      return false;
    }
  }
  return true;
}
Amo
sly sonnet
#

helper function