• Post Reply Bookmark Topic Watch Topic
  • New Topic

random shuffle file  RSS feed

 
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about generating random number rather than shuffling the actual file records?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!