Java Program to Shuffle Vector Elements
Vectors basically fall in legacy classes but now it is fully compatible with collections. Java has many built-in functions to perform different operations on collections or other data types and one of them is shuffle. To shuffle Vector elements Collections.shuffle() method is used. It shuffle method of the Collections class shuffles the elements of the specified Vector object using the default source of the randomness. It randomly permutes the Vector elements passed in parameters.
Application of shuffle() method
- It is used in cryptographic applications.
- Generating unique transaction numbers for a payment field.
- The software in rockets, satellites, airplanes, cryptography utilizes randomization to get a high probability of good results on an algorithm.
Collections shuffle function can also be called in two ways:
- The random parameter to specify randomness.
- Without parameter.
The shuffle method uses the default randomness source to select random elements from the Vector. This function here doesn’t take much time and runs in linear time and each time executed the result can be different.
java ↳ util ↳ Collections
Parameters: The Vector which you will pass will be shuffled.
Returns: Shuffle function shuffles the Vector element.
Original Vector : [5, 6, 7, 8, 9] After shuffling, Vector : [8, 9, 5, 6, 7]
Shuffling a Vector using Random Function which will become the source of Randomness.
Collections.shuffle(Vector, Random random)
Original Vector : [geeksforgeeks, course, practice, archive, interview] Shuffled Vector with Random() : [interview, practice, geeksforgeeks, archive, course] Shuffled Vector with Random(3) : [archive, practice, interview, geeksforgeeks, course] Shuffled Vector with Random(5) : [geeksforgeeks, practice, course, archive, interview]
Please Login to comment...