can't you:
- Build a list of all file paths.
- Shuffle the list once.
- Iterate sequentially through the shuffled list.
- When exhausted, reshuffle again (optional).
This guarantees:
- every image appears exactly once per cycle
- no duplicates before all images are shown
- O(n) setup cost
- O(1) per image retrieval afterward
here it is a VERY stupid C example:
Code:
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
void shuffle(char *arr[], int n)
{
for (int i = n - 1; i > 0; --i)
{
int j = rand() % (i + 1);
char *tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
int main(void)
{
char *images[] = {
"img1.jpg",
"img2.jpg",
"img3.jpg",
"img4.jpg",
"img5.jpg"
};
int n = sizeof(images) / sizeof(images[0]);
srand((unsigned)time(NULL));
shuffle(images, n);
// show all images exactly once
for (int i = 0; i < n; ++i)
{
printf("%s\n", images[i]);
}
return 0;
}
now I see that you are using RUST:
Code:
extern crate rand;
use rand::rng;
use rand::seq::SliceRandom;
fn main() {
let mut images = vec![
"img1.jpg",
"img2.jpg",
"img3.jpg",
"img4.jpg",
"img5.jpg",
];
let mut r = rng();
// shuffle once
images.shuffle(&mut r);
// show all images exactly once
for img in images {
println!("{}", img);
}
}
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=bf21ee09177a42b6d9a1245f59fe1fc4