🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.debian.user
Article #254

Re: Bash: check if a value is into an array

#254
From: Greg Wooledge
Date: Thu, 22 Oct 2026 15:50
24 lines
3171 bytes
On Thu, Oct 22, 2026 at 15:34:59 -0000, CGS wrote:
> If I select random on a finite song playlist, I wouldn't want the same song
> playing twice; this would require an algorithm that would deselect songs that
> have already been chosen. Maybe your code accounted for that (or not).

This is covered by keeping track of what's already been selected,
either in a second array (which is the more efficient way), or by
traversing the playlist to see if the new selection is already in
the playlist (the less efficient way).

With a very small library, this works well enough.  If you have 10
songs, and you generate a playlist by choosing random numbers from
1 to 10 over and over until every number has been selected, it
shouldn't take too long.

With a large library, however, this is very inefficient.  With 10,000
songs, you only have a 1 in 10,000 chance of hitting that final song.
You're going to end up looping hundreds of thousands of times, instead
of only 10,000 times.

If you want a playlist that's as long as your entire library, you should
use a shuffling algorithm instead.  Load up the array, and then make a
single pass through the array, swapping items.  The code to do this is
in my previous message in this thread.
Technical Details
Message-ID: <NR3df-7d0H-1@gated-at.bofh.it>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomod
References: <NQFND-6ZMU-9@gated-at.bofh.it> <NQG6Z-703J-7@gated-at.bofh.it> <NQJod-71PP-13@gated-at.bofh.it> <NQJxT-71Ty-3@gated-at.bofh.it> <NQJRf-729Y-7@gated-at.bofh.it> <NR33A-7cWF-3@gated-at.bofh.it>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.samoylyk.net!gothmog.csi.it!bofh.it!news.nic.it!robomod
From: Greg Wooledge <greg@wooledge.org>
Newsgroups: linux.debian.user
Subject: Re: Bash: check if a value is into an array
Date: Thu, 22 Oct 2026 17:50:01 +0200
Message-ID: <NR3df-7d0H-1@gated-at.bofh.it>
References: <NQFND-6ZMU-9@gated-at.bofh.it> <NQG6Z-703J-7@gated-at.bofh.it> <NQJod-71PP-13@gated-at.bofh.it> <NQJxT-71Ty-3@gated-at.bofh.it> <NQJRf-729Y-7@gated-at.bofh.it> <NR33A-7cWF-3@gated-at.bofh.it>
X-Mailbox-Line: From debian-user-request@lists.debian.org  Thu Oct 22 15:43:17 2026
Old-Return-Path: <greg@wooledge.org>
X-Amavis-Spam-Status: No, score=-1.9 tagged_above=-10000 required=5.3
 tests=[BAYES_00=-1.9] autolearn=ham autolearn_force=no
X-Policyd-Weight: using cached result; rate: -4.6
Mail-Followup-To: debian-user@lists.debian.org
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
User-Agent: Mutt/1.10.1 (2018-07-13)
X-Mailing-List: <debian-user@lists.debian.org> archive/latest/832564
List-ID: <debian-user.lists.debian.org>
List-URL: <https://lists.debian.org/debian-user/>
List-Archive: https://lists.debian.org/msgid-search/20261022154302.GF28455@wooledge.org
Approved: robomod@news.nic.it
Lines: 24
Organization: linux.* mail to news gateway
Sender: robomod@news.nic.it
X-Original-Date: Thu, 22 Oct 2026 11:43:02 -0400
X-Original-Message-ID: <20261022154302.GF28455@wooledge.org>
X-Original-References: <fb4588c5-2637-4714-81e6-791a9260afbd@gmail.com>
 <20261021150743.GA21484@wooledge.org>
 <b397ac30-dbff-4822-b314-c6cc97dc0df8@gmail.com>
 <atkI_Agk3vuY5vlg@phare.normalesup.org>
 <20261021190728.GB28455@wooledge.org>
 <slrn11dkb93.1hg.etphonehomefrance@einstein.home.arpa>