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;
}

Recommended Answers

All 6 Replies

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.

Said problem is confusingly described by party of the first part in an attempt to impart wisdom as to salient description of said problem.

Do you mean "I want to figure out all the possible string combinations of a given set of characters?"

Are you in fact having a problem? I see no description of the difficulty you are having, just a task description.

And please format your code. When you need help from others, said others must be able to understand code posted. Formatting aids in said understanding.

commented: very nice +1

Sorry my English is bad.The problem im having is:
A.the number generator is repeating the number it generates alot how can it be made more random on every call.

B.The program is generating alot of matching combinations of words and is not calculating all the possible combination of words.

#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define sec_to_wait 10 // time set to look for combinations 
#define default_str_size 3

using namespace std;

int rgennum(int min,int max);

int main()
{
time_t mytime;
vector<string> ranstr;
double nstr = 0;//num of combinations so far
string str;
bool found= 0;
double matches = 0;
int strsize =  default_str_size;

cout<<"enter string lenght to calculate number of possible combinations ?"<<endl;
int usize = 0;
cin>>usize;

if(usize !=default_str_size)
strsize = usize;


//start the the stopwatch
time_t start,end;
time(&start);
cout<<"time started processing "<<ctime(&start);

while(true)
{

//chk the time here if time is up then break
time(&end);
double t_differnce = difftime(end,start);
if(t_differnce == sec_to_wait)//if its time to end
{
cout<<"end time processing "<<ctime(&end);
cout<<"number of possible string combination so far generated "<<nstr<<" avalible for string lenght "<<strsize<<endl;;
cout<<"number of matching string generated but discarded "<<matches<<endl;;
cin.get();
break;
}

//make a random string  for strsize
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 in vector
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;
}

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;
}

Sorry my English is bad.

Yeah, you sound like a lawyer. They can't talk English either.

The problem im having is:
A.the number generator is repeating the number it generates alot how can it be made more random on every call.

At the beginning of the program you forgot the srand() function.

B.The program is generating alot of matching combinations of words and is not calculating all the possible combination of words.

Example please....

I repeat -- forcefully --

And please format your code. When you need help from others, said others must be able to understand code posted. Formatting aids in said understanding.

I for one am not looking at code that is not formatted. See the link from the last request.

is not calculating all the possible combinations

Why do you think it should?
PS: your rgennum wastes a lot of time, and does not generate true randoms. I'd rather have it as

int rgennum(int min, int max)
{
    return min + rand() % (max - min);
}

What algorithm do i need that will generate every possible combination of a word of a set length from the user ?

What i want to do is store every combination possible in a vector then write it to text file.

Thx for the advice so far.

What algorithm do i need that will generate every possible combination of a word of a set length from the user ?

Typically it is done by recursion: for each letter in the word use this letter as a head, and build all possible tails applying the same algorithm to the rest of letters. Some attention is required for non-unique letters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.