mercredi 19 octobre 2022

More "random" alternative to shuf for selecting files in a directory

I put together the following Bash function (in my .bashrc) to open a "random" image from a given folder, one at a time until the user types N, after which it exits. The script works fine aside from the actual randomness of the images generated - in a quick test of 10 runs, only 4 images are unique.

Is this simply unavoidable due to the limited number of images in the directory (20), or is there an alternative to the shuf command that will yield more random results?

If it is unavoidable, what's the best way to adapt the function to avoid repeats (i.e. discard images that have already been selected)?

function generate_image() {
    while true; do
        command cd "D:\Users\Hashim\Pictures\Data" && 
        image="$(find . -type f -exec file --mime-type {} \+ | awk -F: '{if ($2 ~/image\//) print $1}' | shuf -n1)" &&
        echo "Opening $image" &&
        cygstart "$image"
        read -p "Open another random image? [Y/n]"$'\n' -n 1 -r
        echo
        if [[ $REPLY =~ ^[Nn]$ ]] 
        then exit
        fi 
    done
}



Aucun commentaire:

Enregistrer un commentaire