Manipulating Random Word Generation

Hello,

I would like to know how I can manipulate the results for a string such that 20% of the time a string of 7 words of which one is a symbol.
This is my code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <algorithm>


using namespace std;

const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";


int main() {
	srand(time(0));

	string words;

	for(int z = 0; z < 7; z++) words+=letters.at(rand()% 51);
	cout << words;

	return 0;
}
Hello CelestialX,

I am not understanding what you need. Is it one string of seven different words or as you code make one word of seven different letters? If you want seven different words you could do something like this:
1
2
3
4
5
6
for (int lp = 0; lp < 7; lp++)
{
	for (int z = 0; z < 7; z++) words += letters.at(rand() % 51);
	words += " ";
}
	cout << words << endl;


Of course this does nothing to include the symbol you mentioned.

Hope this helps,

Andy
my humble apologies but I do not understand what you ask. Please provide more detail.
sorry my apologies, so I am trying to figure out how I can produce a string of 7 random characters that has a 10% chance of mixture of numbers and letters, 10% chance of getting a mixture of symbols and letters, and 80% chance of getting just letters. This is my improvised code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <algorithm>


using namespace std;

const string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const string wcard1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
const string wcard2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*";


int main() {
	srand(time(0));

	string words;

	int chance = rand() % 10;

	if (chance == 8){
		for(int i = 0; i < 7; ++i) words += alphabets.at(rand()% 51);
		cout << words;
	}

	else if (chance == 2){
		chance = rand() % 2;
		if (chance == 1){
			for(int j = 0; j < 7; ++j) words += wcard1.at(rand() % 61);
					cout << words;
		}
		else if (chance == 0){
			for(int k = 0; k < 7; ++k) words += wcard2.at(rand() % 59);
					cout << words;
		}
	}

	return 0;
}
I figured a way to fix it: I replaced the else if s with just else statements.
So I am trying to make typing game where the computer generates a string of mentioned specs, then User must type the same string, however if the User misspells then he gets penalty like |G - M| = 6

This is my code so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <ctime>
#include <algorithm>


using namespace std;

const string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const string wcard1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
const string wcard2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*";


int main() {
	srand(time(0));
	
	while(true){

		string words;

		int chance = rand() % 10;

		if (chance == 8){
			for(int i = 0; i < 7; ++i) words += alphabets.at(rand()% 51);
			cout << words;
		}

		else {chance = rand() % 2;
			if (chance == 1){
				for(int j = 0; j < 7; ++j) words += wcard1.at(rand() % 61);
					cout << words;
			}
			else {
				for(int k = 0; k < 7; ++k) words += wcard2.at(rand() % 59);
					cout << words;
		}
	}
		string input;
		
		
	
	}

	return 0;
}
Topic archived. No new replies allowed.