hi
Im trying to make this code go though every possible string combination at said string length though alpha characters using random number generator to do so.The goal is to make this program tell me how many combinations there are at said circumstances set by user.
#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define sec_to_wait 10//time to process for
using namespace std;
const int strsize = 2;
int rgennum(int min,int max);
int main()
{
time_t mytime;
vector<string> ranstr;
int nstr = 0;
string str;
int count =0;
bool found= 0;
int matches = 0;
//start the the stopwatch
time_t start,end;
time(&start);
cout<<"time started "<<ctime(&start);
while(true)
{
//chk the time here if its time then break
time(&end);
double t_differnce = difftime(end,start);
if(t_differnce == sec_to_wait)//if its time to end
{
cout<<"end time "<<ctime(&end);
cout<<"number of possible str combination "<<nstr<<" avalible for str lenght "<<strsize<<endl;;
cout<<"number of matching str generated but discarded "<<matches<<endl;;
cin.get();
break;
}
//make a random string at said size
for(int t=0;t<strsize;t++)
{
char c = (char)rgennum(97,122);
str.push_back(c);
}
//chk to see if str has been generated before accepting it
for(int t =0;t<ranstr.size();t++)
{
// if a match
if(str.compare(ranstr[t]) == 0)
{
found = true;
matches +=1;
break;
}
}
if(!found)
{
ranstr.push_back(str);
nstr+=1;
count+=1;
}
found = 0;//assume the next str will not be found
str.erase();
}
cin.get();
return 0;
}
int rgennum(int min,int max)
{
bool bad = 1;
int rnum = 0;
int lastgen = 0;
while(bad)
{
lastgen = rnum;
//gen rand num
srand(GetTickCount());
rnum = rand()%max; // specifies the random number range
if(rnum==lastgen)
continue;
else if(rnum < min)
continue;
else
bad = 0;
}
return rnum;
}