Reputation:
I want to write a function which randomizes the order of a sequence of alphabetic characters. For example, the sequence:
A B C D E F G . . .
...might be changed to:
Z L T A P ...
...which, if passed to the same function again could result in:
H R E I C ....
Any suggestions?
Upvotes: 1
Views: 471
Reputation: 837926
Have a look at the Fisher-Yates shuffle algorithm, and in particular the modern version of it.
Upvotes: 2
Reputation: 24502
You mean randomise the alphabet? I wrote something similar in PHP a few days ago. The logic was the following:
The result is a randomly shuffled set of characters, created with minimal CPU load (if the string has 26 characters, the inner loop only needs 26 iterations).
Upvotes: 0
Reputation: 218798
This sounds like homework, but either way:
http://stanford.edu/~blp/writings/clc/shuffle.html
Upvotes: 0