C++ Programming

Random Alphanumeric

CPP example for random alphanumeric

11/8/2019
0 views
random-alphanumeric.cppCPP
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

int main() {

	/* Decleration and initialization of static constant string type variable */
	static const string charList = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	/* srand() initialize random number generator */
	/* time() for get current time */
	srand(time(0));
	string alphanumeric = "";
	
	for(int i = 0; i < 8; i++) {
		/* rand() generate random number */
		alphanumeric += charList [rand() % charList.size()];
	}
	
	cout << "Random Alphanumeric: " << alphanumeric << endl;
	return 0;
}


/* Output */
Random Alphanumeric: m7csZCs7
random alphanumericC plus plusfor loop

Related Examples