• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with creating a random array.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So first I had to create an insert method to place tracks into an array.. Got that easy enough.. Next I have to randomly mix up that array... Can get some of that but my last track is being duplicated a lot of the time.. I heed help to stop this! Here is my code!





list is the original array that the tracks were inserted into.
entryCount is the list length.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thomas,

Why don't you just use Lists instead of arrays? You will also be able to simply use the Collections.shuffle() method.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The standard Java library already has a method to shuffle a List: Collections.shuffle(list). If you need to shuffle an array, you can use Arrays.asList(array) to wrap the array as a list.

So to shuffle your array of Track objects you could just do:

If you need to implement this yourself (for example because it's homework and you're supposed to implement it yourself), see Fisher–Yates shuffle for an explanation of a good and simple shuffling algorithm.
 
reply
    Bookmark Topic Watch Topic
  • New Topic