Hey there.
Can you help me with a question, how should I add the “random” option to a set of tracks. I would like that the app chooses an audio randomly from a list to be played.
I haven’t been able to find anything related to this. Thanks a lot.
Hey there.
Can you help me with a question, how should I add the “random” option to a set of tracks. I would like that the app chooses an audio randomly from a list to be played.
I haven’t been able to find anything related to this. Thanks a lot.
There are two basic approaches to this, depending on how you feel about coverage:
If you don’t care at all about coverage (i.e. it’s OK if track 2 plays 7 times before track 8 plays even once), then simply call this over and over again:
chooseRandomTrack(tracks: Track[]): Track {
return tracks[Math.floor(Math.random() * tracks.length))];
}
If you want to ensure that any given track is not played more frequently than any other, then treat it like a deck of cards: shuffle the deck (Fisher-Yates is the canonical algorithm for this), and then choose tracks sequentially out of the shuffled deck.