-1

I want to add a shuffle option in my android music player app. For that purpose i am calling a random function in java to return a number between 0 and size of the currently playing songs list. The problem is that the function returns same value as before and repetitions happens in the value. There are many solutions available in stack overflow herself to avoid this problem. That solutions as well as the following will not work in my case. 1. Creating a shuffled array contains 0 to maximum list size value. It will fail because the size of the songs list is dynamic. 2. Shuffling the songs list array itself. it will fail because of the time complexity.

What i need is a mathematical hash function which will do the following..

If 50 is the maximum range, then the input number 1 will map to any other number say 34. Input 2 to any other number say 21. Input 3 to any other number say 10 Etc. All the mapped numbers should be within the range 50 and no repetitions are allowed. (No coalition while mapping ) also mapping to that number is not allowed. Please note that if the maximum range is same (in this case 50)and the input number 2 to this function should return the value 21 at anytime throughout the application.

The function time complexity should be less.

Shanij P.S
  • 137
  • 3
  • 8
  • 7
    You don't need all that math, just sort the list randomly in the beginning and then just play one song after another, that's how basically all shuffle features work – Xaver Kapeller Jul 28 '14 at 17:49
  • Collections.shuffle(yourMusicList); – gtgaxiola Jul 28 '14 at 17:50
  • Just use the existing `Collections.shuffle()`. http://stackoverflow.com/questions/969573/java-random-collection – Sky Kelsey Jul 28 '14 at 17:51
  • @gtgaxiola that's it, a simple one liner – Xaver Kapeller Jul 28 '14 at 17:51
  • Yup, what Xaver said is the right answer. Creating a new data structure, and having the possibility that you may never get all of the elements of your list mean that it's MUCH more efficient to come up with the list of numbers and shuffle to get the order. – StoneMan Jul 28 '14 at 17:56
  • 1
    I have already mentioned that Collection.shuffle is not feasible. I can manipulate my list at any time like delete, insertion etc.. This affect the consultancy of the user interface of the app like playing 10 th song but viewing 4th song information. – Shanij P.S Jul 28 '14 at 17:57
  • Nothing says you have to shuffle the **original** playlist; you can shuffle a copy and then when playlist modifications occur you simply need to keep the two copies in sync by performing an equivalent action on the shuffled copy (e.g. when you delete a song from the playist, you need to perform a deletion from the shuffled copy; when you insert into the playist, you need to insert at a random location within the copy). Your user interface will still be consistent, because it will show the (unsorted playlist), with the currently-playing song highlighted; the sorted list never goes on the UI. – Tim Jul 28 '14 at 18:21
  • 1
    Reading through your requirements for this hash function, it appears you want this to be repeatable (e.g. 2 hashes to 21 every time). Why? Do you really want your music player to have a shuffle feature that's not random and that always plays songs in the same order? And why is it forbidden for any number to hash to itself; isn't it enough that most numbers hash to something else (giving your user the experience of randomness) even if one or two happen to hash to themself? Even if someone gave you the hash function you've asked for, I'm not sure that it would be what you really want. – Tim Jul 28 '14 at 18:28

1 Answers1

0

You can still use Collections.shuffle() initially. As you continue playing music, keep track at what index the current song is at. So lets say you've played 7 songs and are currently on the 8th. Then curr=7 When you want to delete a song, find the song in the list and remove it. If you want to insert a song, randomly generate a numbers between curr + 1 and songList.size(). Insert your song here using songList.add(index, element). If you feel the shifting is too time expensive, then you can copy the val at this index, add it to the end of the list, and insert the new song at that index.

mistahenry
  • 8,064
  • 3
  • 22
  • 36