I'd like to create a random string, consisting of alpha-numeric characters. I want to be able to be specify the length of the string.
How do I do this in C++?
I'd like to create a random string, consisting of alpha-numeric characters. I want to be able to be specify the length of the string.
How do I do this in C++?
Example for Qt use:)
Be ware when calling the function
(adapted of @Ates Goral) it will result in the same character sequence every time. Use
before calling the function, although the rand() function is always seeded with 1 @kjfletch.
For Example:
Let's make random convenient again!
I made up a nice C++11 header only solution. You could easily add one header file to your project and then add your tests or use random strings for another purposes.
That's a quick description, but you can follow the link to check full code. The main part of solution is in class Randomer:
Randomer
incapsulates all random stuff and you can add your own functionality to it easily. After we haveRandomer
, it's very easy to generate strings:Write your suggestions for improvement below. https://gist.github.com/VjGusev/e6da2cb4d4b0b531c1d009cd1f8904ad
Mehrdad Afshari's answer would do the trick, but I found it a bit too verbose for this simple task. Look-up tables can sometimes do wonders: