Random Playlist

Hello,

I am trying to make a playlist, with 6 songs. My question is how can I have the 6 songs, in a function or some sort and be able to play each song randomly. I do not want it to play the songs in the same order, randomly. I’ve tried adding all the songs separately in function, then trying to randomize the songs in the function but that did not work. Also, I don’t think it will know when one song is over, their all different times.

Thanks for your help. I am not sure how i can implement this :o

  • Michael [import]uid: 23689 topic_id: 20422 reply_id: 320422[/import]

Well you would normally put the list of songs in a table and if you’re wanting something like shuffle mode, where each song plays once, then you need a shuffle routine.

This one is posted a lot in other forum posts on shuffling:

local function shuffle(t)  
 local random = math.random  
 local iterations = #t  
 local j  
  
 for i = iterations, 2, -1 do  
 j = random(i)  
 t[i], t[j] = t[j], t[i]  
 end  
end  

then just:

local playList = {“song1.mp3”, “song2.mp3”, “song3.mp3” }

shuffle(playList)

[import]uid: 19626 topic_id: 20422 reply_id: 79955[/import]

Hello,

Thanks for your help Rob! But I have tried it and not any success, as you said I should have something like this? This is exactly what I have below.

Thanks again!

[code]
local function shuffle(t)
local random = math.random
local iterations = #t
local j

for i = iterations, 2, -1 do
j = random(i)
t[i], t[j] = t[j], t[i]
end
end

local playList = {“Francoisgame4.mp3”, “Francoisgame2.mp3”, “Francoisgame1.mp3” }
shuffle(playList) [import]uid: 23689 topic_id: 20422 reply_id: 79958[/import]

have you dropped a for loop with a print after it to see that the table changed?

  
for i = 1, #playList do  
 print(i,playList[i])  
end  

[import]uid: 19626 topic_id: 20422 reply_id: 79959[/import]