1

So i am creating a "music library", basically you add "your song" to the first list and then move the song you want to the other list, being the playlist. In the playlist i would like to be able to randomize it since i already have a button for sorting it.

I should add that i am using, DefaultListModel model1 = new DefaultListModel(); in the coding. I'm sorry if i am not very good at explaining myself, i dont really know how to describe it.//

Yo Pro
  • 13
  • 4

3 Answers3

2

If you need to shuffle a list as you say, there is a method to do this through Collections:

Collections.shuffle(nameOfYourList);

This'll rearrange your list randomly.

You can read more about this here.

Alex K
  • 8,269
  • 9
  • 39
  • 57
  • Java uses a [Linear congruential generator](http://en.wikipedia.org/wiki/Linear_congruential_generator) and when you manually provide the seed you can introduce statistically significant (and predictable) changes to the initial few values. – Elliott Frisch Jan 11 '15 at 14:58
  • @ElliottFrisch Interesting. Thanks for the comment. I didn't know about that. – Alex K Jan 11 '15 at 14:59
1
ArrayList<Integer> array = new ArrayList<Integer>;
array.add(1);
array.add(2);
array.add(3);
array.add(4);
array.add(5);
int[] temp = array.size;
for(int i = 0; i < array.size; i++){
    int r = (int) (Math.random()*array.length);
    temp[r] = array.get(r);
    array.remove(r);
}

You can do a simple arraylist. Arraylist are very easy.

Mert Karakas
  • 178
  • 1
  • 4
  • 22
-1

Make a random int using the Random class between 0 and your total number of songs, check that the slot relating to it is empty in your list, if it is then add your song there otherwise repeat the proccess. Go through all the songs like this and each one will be assigned randomly. e.g:

Random r = new Random();
for (Music m : unrandomList) {
    while (true) {
       int index = r.nextInt(list.size());
       if (list.getElementAt(index) != null) {
          list.add(index,Music);
          break;
       }
    }
}
Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
dodo
  • 156
  • 10