File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Need help with creating a random array. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Need help with creating a random array." Watch "Need help with creating a random array." New topic
Author

Need help with creating a random array.

Thomas Mullane
Greenhorn

Joined: Jan 29, 2012
Posts: 3
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.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3050
    
    1

Hi Thomas,

Why don't you just use Lists instead of arrays? You will also be able to simply use the Collections.shuffle() method.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12921
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Need help with creating a random array.
 
Similar Threads
(anti)clockwise rotation of array elements...HELP!
breaking out of while loop without condition being met?
Please help in this selection sort
In Dire Need of Data Structure Advice
What is wrong with my generic implementation of sort method ?