Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
crawfishmedia
Binge Watcher

Randomize playlist

Hi guys
I am trying to randomize the entries in a photo playlist array. It seems to work fine except for some reason the last 3-4 entries do not randomize to the new array - they're chopped off.
I am looping through the playlist array, and after I randomly find and copy an entry to a new array, I delete the array entry so it won't be used again.

According to the documentation, the RND function should randomize between 0 and 1. So if I perform RND(1), shouldn't the result be 0 or 1? Or, RND(3) result be 0,1,2, or 3?

Here is a sample of my print output:

ext1:/Photos/1.jpg
ext1:/Photos/2.jpg
ext1:/Photos/3.jpg
ext1:/Photos/4.jpg
ext1:/Photos/5.jpg
ext1:/Photos/6.jpg
ext1:/Photos/7.jpg
ext1:/Photos/8.jpg
ext1:/Photos/9.jpg
ext1:/Photos/10.jpg

Length of playlist = 10

(Length of playlist : random array position > new array position)
10 : 7 > 0
9 : 2 > 1
8 : 6 > 2
7 : 1 > 3
6 : 5 > 4
5 : 2 > 5

Sample code:

index = 0
for each item in playlist_master
' Calculate the new length of the master playlist
playlist_master_count = playlist_master.count()
if playlist_master_count > 0
' Get a random index value
random_index = RND(playlist_master_count)
' Get playlist item from master playlist based on random index value
playlist_item = playlist_master[random_index]
' Copy the playlist item to the new randomized playlist
playlist_random.Push(playlist_item)
' Remove the copied playlist item from master so it isn't copied again
playlist_master.Delete(random_index)
else
'Just copy last entry'
playlist_item = playlist_master[playlist_master_count]
playlist_random.Push(playlist_item)
playlist_master.Delete(playlist_master_count)
end if
' Log playlist processing
print playlist_master_count;" :";random_index;" >";index
index = index + 1
end for


Thanks for any suggestions
AC
0 Kudos
5 REPLIES 5
TheEndless
Channel Surfer

Re: Randomize playlist

Rnd(3) will produce values of 1, 2, or 3. Never 0, so you should pull your random index with:
random_index = RND(playlist_master_count) - 1
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
crawfishmedia
Binge Watcher

Re: Randomize playlist

"TheEndless" wrote:
Rnd(3) will produce values of 1, 2, or 3. Never 0, so you should pull your random index with:
random_index = RND(playlist_master_count) - 1


Thanks for the -1 tip, which makes total sense in order to reach the array at index [0].

My loop still exits with 3 or 4 items remaining if I use a random index. If I delete items at index [0] on each loop instead, all the items in the array are transferred to the new array.

I created a separate array with static items just to test this. I get the same strange result where the loop does not finish.

Losing my mind.
0 Kudos
crawfishmedia
Binge Watcher

Re: Randomize playlist

Found the answer.

This loop will work:
for i=1 to playlist.count() step 1

This loop will not, it will cut itself short for some unknown reason: (to waste my time)
for each item in playlist.count()
0 Kudos
RokuMarkn
Visitor

Re: Randomize playlist

foreach won't work correctly if you delete entries in the array while traversing it. You may want to look at the Fisher-Yates shuffling algorithm, which is a simple way to shuffle an array in-place.


function Shuffle(array as Object) as Void
for i% = array.count()-1 to 1 step -1
j% = Rnd(i% + 1) - 1
t = array[i%] : array[i%] = array[j%] : array[j%] = t
end for
end function


--Mark
0 Kudos
crawfishmedia
Binge Watcher

Re: Randomize playlist

"RokuMarkn" wrote:
foreach won't work correctly if you delete entries in the array while traversing it. You may want to look at the Fisher-Yates shuffling algorithm, which is a simple way to shuffle an array in-place.


function Shuffle(array as Object) as Void
for i% = array.count()-1 to 1 step -1
j% = Rnd(i% + 1) - 1
t = array[i%] : array[i%] = array[j%] : array[j%] = t
end for
end function


--Mark


Thanks a lot for this idea! From 14 to 4 lines of code, nice!

AC
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.