stephenmattison
BAN USERIt depends on what he means by "randomly". It is impossible to be truly random from the onset of the problem without knowing how many songs are on each CD.
Here is what I would do:
To be truly random we would just have a list of all songs, randomly choose an integer in the range of [0,n) where n is the number of songs, pick that song, remove it from the list, and decrement n. When n == 0 start over. For this to work we need to know all of the songs ahead of time, which we can't. So we have to fake it.
Determine ahead of time the "average" number of songs per CD, denoted as C. This will be our starting point. Construct a list of "predicted songs", for each CD in the changer assume that there are C songs. Enumerate these C songs per CD in a list.
Now do the name procedure picking an integer I from [0,n), tracking to the respective CD. Once on the CD we will lookup how many songs, and transform I into the range of the number of songs, for instance if we choose track 10 on CD 5, and CD 5 has 3 songs, well then we pick the 3*(10/C) song where C is the average number of songs that we expected. After we have this additional information, update our global list of "expected" songs removing the one we already played. In this manner eventually we will discover how many tracks are on each CD, and play them all.
This isn't really random as you could have a CD with 1000 songs, and 100 CD's with 1 song, yet you would pick them all equally.
Nevertheless, this is a good solution.
Exactly, probably just to figure out if you have very basic coding skills, i.e. reading from files, using hash tables, etc.
- stephenmattison June 09, 2009