Thursday, January 7, 2016

Random string generator C++

I wrote same function in C++ that I had written in BASIC to create a random string.

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;

string a;
string b;
string c;
string d;
string e;
string f;
string g;
string h;
string i;
string j;
string k;
string l;
string m;
string n;
string o;
string p;
string q;
int RandomNumber1;
int RandomNumber2;
int RandomNumber3;
int RandomNumber4;
int RandomNumber5;
int RandomNumber6;
int RandomNumber7;
int RandomNumber8;
int RandomNumber9;
int RandomNumber10;
int RandomNumber11;
int RandomNumber12;
int RandomNumber13;
int RandomNumber14;
int RandomNumber15;
int RandomNumber16;
int RandomNumber17;


string AlphaRandom[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
int main()
{

srand(time(0));

RandomNumber1 = rand() % 51 + 0;
string a = AlphaRandom[RandomNumber1];

RandomNumber2 = rand() % 51 + 0;
string b = AlphaRandom[RandomNumber2];

RandomNumber3= rand() % 51 + 0;
string c = AlphaRandom[RandomNumber3];

RandomNumber4= rand() % 51 + 0;
string d = AlphaRandom[RandomNumber4];

RandomNumber5= rand() % 51 + 0;
string e = AlphaRandom[RandomNumber5];

RandomNumber6= rand() % 51 + 0;
string f = AlphaRandom[RandomNumber6];

RandomNumber7= rand() % 51 + 0;
string g = AlphaRandom[RandomNumber7];

RandomNumber8= rand() % 51 + 0;
string h = AlphaRandom[RandomNumber8];

RandomNumber9= rand() % 51 + 0;
string i = AlphaRandom[RandomNumber9];

RandomNumber10= rand() % 51 + 0;
string j = AlphaRandom[RandomNumber10];

RandomNumber11= rand() % 51 + 0;
string k = AlphaRandom[RandomNumber11];

RandomNumber12= rand() % 51 + 0;
string l = AlphaRandom[RandomNumber12];

RandomNumber13= rand() % 51 + 0;
string m = AlphaRandom[RandomNumber13];

RandomNumber14= rand() % 51 + 0;
string n = AlphaRandom[RandomNumber14];

RandomNumber15= rand() % 51 + 0;
string o = AlphaRandom[RandomNumber15];

RandomNumber16= rand() % 51 + 0;
string p = AlphaRandom[RandomNumber16];

RandomNumber17= rand() % 51 + 0;
string q = AlphaRandom[RandomNumber17];

std::string s = static_cast<std::ostringstream&>(std::ostringstream().seekp(0) << a << b << c << d << e << f << g << h << i << j << k << l << m << n << o << p << q).str();

cout << s;
    return 0;
}

No comments:

Post a Comment