View Single Post
  #11  
Old 05-27-2026, 11:54
wild wild is offline
Friend
 
Join Date: Oct 2017
Posts: 37
Rept. Given: 0
Rept. Rcvd 1 Time in 1 Post
Thanks Given: 54
Thanks Rcvd at 32 Times in 17 Posts
wild Reputation: 1
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

Last edited by wild; 05-27-2026 at 13:31.
Reply With Quote
The Following User Says Thank You to wild For This Useful Post:
tonyweb (05-31-2026)