Generating Random Permutations
Part of what I am currently working with involves a SQL command parser/reassembler and anytime you are faced with something along these lines, covering as many permutations as you can becomes important. The problem becomes when the list of items facing you is more than a few.
For MS’s implementation of ‘INSERT’ there are 10 table hints which can be provided to assist the server in doing what you want. Given the number of possible permutations from this is waaaay too large to even automate every one, there is not much choice other than to do a random sampling.
My first approach was to use some of the python permutation generators that google could find, but they all started from the beginning of the list and incremented by one through the list. Not very random.
Trying to write as little code as possible I figured I would just make one large list and pick random entries from it. Bad idea. Python is definitly not meant for this kind of task and brought my machine’s performance to a near halt.
Admitting this path was the road to ruin, I broke down and wrote some code from scratch. A few minutes later, the script below appeared and does exactly what I need: Generate a string which contains a random number of hints without repeat or built-in order.
import random
def hint_creator(hint_list):
built_hints = []
total_hints = len(hint_list)
built_len = random.randint(1, total_hints)
looper = 0
while looper
Feel free to use this pattern (for lack of a better term) to generate your own test data.