View Single Post
  #12   Spotlight this post!  
Unread 01-08-2012, 11:11
JesseK's Avatar
JesseK JesseK is online now
Expert Flybot Crasher
FRC #1885 (ILITE)
Team Role: Mentor
 
Join Date: Mar 2007
Rookie Year: 2005
Location: Reston, VA
Posts: 3,688
JesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond reputeJesseK has a reputation beyond repute
Re: Replacing duplicates in a randomly generated array

I'll note that I've used Greg McKaskle's last solutions in several simulation and test applications in Java by using something similar to this:
Code:
LinkedList<Object> someList = getList();
Collections.rotate(someList, (int)(Math.random()*(someList.size() - 1)));
Object randomObject = someList.remove();
  • Rotating is usually much faster than shuffling
  • Removing the element from the queue ensures lack of duplicates
If you want a good lesson in C, you could try implementing a basic generic rotate function and a basic generic LinkedList object. This isn't the most efficient way in Java since the Collections class doesn't know about the Queue implementation of the LinkedList (thus it could save time by manipulating indices rather than doing a rotation), but implementing your own solution would give you that flexibility.
__________________

Drive Coach, 1885 (2007-present)
CAD Library Updated 5/1/16 - 2016 Curie/Carver Industrial Design Winner
GitHub