vlc-video: Fix shuffle not being quite that random

This changes it so that the chooses indices at random from an index list
until the index list is exhausted.
master
jp9000 2017-10-03 18:42:57 -07:00
parent 3ea23320b8
commit 9321b822bf
1 changed files with 19 additions and 6 deletions

View File

@ -568,14 +568,27 @@ static void vlcs_update(void *data, obs_data_t *settings)
c->shuffle = obs_data_get_bool(settings, S_SHUFFLE);
if (c->files.num > 1 && c->shuffle) {
for (size_t i = 0; i < c->files.num - 1; i++) {
size_t j = i + rand() / (RAND_MAX
/ (c->files.num - i) + 1);
DARRAY(struct media_file_data) new_files;
DARRAY(size_t) idxs;
struct media_file_data t = c->files.array[j];
c->files.array[j] = c->files.array[i];
c->files.array[i] = t;
da_init(new_files);
da_init(idxs);
da_resize(idxs, c->files.num);
da_reserve(new_files, c->files.num);
for (size_t i = 0; i < c->files.num; i++) {
idxs.array[i] = i;
}
for (size_t i = idxs.num; i > 0; i--) {
size_t val = rand() % i;
size_t idx = idxs.array[val];
da_push_back(new_files, &c->files.array[idx]);
da_erase(idxs, val);
}
da_free(c->files);
da_free(idxs);
c->files.da = new_files.da;
}
/* ------------------------------------- */