Refactored string generation to avoid some pitfalls

The original implementation of string generation made it difficult to
handle generating all strings while maintaining the ability to randomly
generate a string.