Two Laptop Bag
The moose likes Beginning Java and the fly likes random shuffle file 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 "random shuffle file " Watch "random shuffle file " New topic
Author

random shuffle file

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
Hello all
i file with 10000 records and need to shuffle the file so each record
will be set in different place
what is the best way to do it ?
Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

With a truly random shuffle it is allowed to keep some elements in the same place. In fact, there is a very very slight chance that the order will remain exactly the same.

Check out java.util.Collections, that has some nice methods you can use.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

I'm not sure you really NEED to do that (do you really?), but if you do then you are looking for a derangement. As the article says, the probability that a random permutation of 10,000 records is actually a derangement is pretty close to 1/e.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9950
    
    6

delete the first record, and re-write it on the end. you are guaranteed to not have any element in the same position.



Seriously, it helps to define the problem better.


Never ascribe to malice that which can be adequately explained by stupidity.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
How about generating random number rather than shuffling the actual file records?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: random shuffle file
 
Similar Threads
Password shuffling
Collections.shuffle question
WA #1.....word association
Shuffleing and array
JPanel or JFrame problem?