On 22/10/26 at 16:22, Greg Wooledge wrote:
For this project, it would be better to shuffle the array of files
instead of selecting one at a time.

#!/bin/bash

# Gather the files.
cd ~/Video/clips/partyzone/ || exit
playlist=([0-9][0-9][0-9][0-9]*)

# Shuffle the playlist.
n=${#playlist[@]}
for ((i=n-1; i>0; i--)); do
     max=$(( 32768 / (i+1) * (i+1) ))
     while (( (rand=RANDOM) >= max )); do :; done
     rand=$(( rand % (i+1) ))
     tmp=${playlist[i]} playlist[i]=${playlist[rand]} playlist[rand]=$tmp
done

# Play the shuffled playlist.
for clip in "${playlist[@]}"; do
     ffplay -vf "scale=720:576,setdar=720/576" -autoexit -volume 10 \
         "$clip" >/dev/null 2>&1
done

You can add your trap if you want it.

Greg, there is something odd in the random number generator, when you have some spare time could you take a look at the code, please:

...
#!/bin/bash

# Gather the files.
cd ~/Video/clips/partyzone/ || exit
playlist=([0-9][0-9][0-9][0-9]*)
declare -a assigned     # Declare an indexed array of name: assigned
# set -x
# Shuffle the playlist.
n=${#playlist[@]}
for ((i=n; i>0; )); do
        max=$(( 32768 / (i+1) * (i+1) ))
        while (( (rand=RANDOM) >= max )); do :; done
        rand=$(( rand % (i+1) ))
        rand=$((RANDOM % n))
        if [ -z "${assigned[rand]}" ]; then
                assigned[$rand]=1
                ((i--))
        else
                continue
        fi
        tmp=${playlist[i]} playlist[i]=${playlist[rand]} playlist[rand]=$tmp
        echo $rand
done
exit

# Play the shuffled playlist.
for clip in "${playlist[@]}"; do
ffplay -vf "scale=720:576,setdar=720/576" -autoexit -volume 10 "$clip" >/dev/null 2>&1
done

I've added the "if [ -z "${assigned[rand]}" ]; then" statement because $rand had many occurrences of the same value so I tried with the "assigned[$rand]" array to avoid that, looping in the circumstance "$rand" had a value already assigned.

The issue is that when I comment out the line "rand=$((RANDOM % n))" and place ((i=n-1; i>0; )) in the "for" statement header, the script runs in an endless loop. :( I don't know why, I tried to enable debug with "set -x" but for me doesn't help, it looks like that the range of generated value is too small to complete "i--" iterations, maybe I'm wrong.

Thanks in advance, kind regards,
--
Franco Martelli

Reply via email to