On Fri, Oct 23, 2026 at 17:26:02 +0200, Franco Martelli wrote:
> On 22/10/26 at 16:22, Greg Wooledge wrote:
> > # 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
That's how the shuffle loop is supposed to be.
> 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))
Here, you've undone all the work from the previous 3 lines, and
overwritten rand, using a selection from an incorrect range.
This is simply wrong. Don't add this line.
> if [ -z "${assigned[rand]}" ]; then
> assigned[$rand]=1
> ((i--))
> else
> continue
> fi
What are you even trying to do here? This makes no sense. The
loop as I wrote it is shuffling the whole array. When it's done,
the array *is* the playlist.
Your addition is building up a second array for no reason, and
messing with the loop counter's value.
Don't add this either.
> 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.
It's *fine* if rand gets the same value more than once in this loop.
It just means that particular slot in the playlist will be swapped
more than once. It doesn't mean a song will be repeated.
I suspect you don't understand what the original code is doing. I added
the comments for a reason -- they tell you what each section does.
I didn't explain every line, because that's not how code comments should
be used.
If you need help understanding the original code (e.g. an explanation
of every single line, one by one), feel free to ask.
Are you stuck on the word "shuffle"? It's normally used with a deck of
playing cards. If you have a deck of cards, and you shuffle it, then
the same cards are still in the deck, but they're in a different, random,
order. You don't magically get two of the same card, unless your deck
started out with duplicates.
That's what this loop is doing. It takes your array of filenames and
shuffles it. The filenames are randomly re-ordered.