Random String Generation

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

string randstr();
int main()
{
	const int s = 12, p = 5;
	int x[s][p], size = 3;
 	string sname;
	
	cout << "\t\t";
	for (int count = 1; count < 6; count++)
	{
		cout << count << "\t";
	}
	cout << endl << endl;
	for (int i = 0; i < s; i++)
	{
		for (int j = 0; j < p; j++)
		{
			x[i][j] = 0 + rand() % 101;
		}
	}
	for (int i = 0; i < s; i++)
	{
		sname = randstr();
		cout << sname << "           ";
		for(int j = 0; j < p; j++)
		{
			cout << x[i][j] << "\t";
			if (j == (p - 1))
			{
				cout << endl;
			}
		}
	}
	unsigned seed = time(0);
	srand(seed);
	
	
	return 0;
	
}

string randstr()
{
	int L = 3;
	string str = "    ";
	for(int c = 0; c < L; c++)
	{
		unsigned seed = time(0);
		srand(seed);
		str[c] = 65 + rand() % 26;
	}
	return str;
	
}


                1       2       3       4       5

TTT            41       85      72      38      80
TTT            69       65      68      96      22
TTT            49       67      51      61      63
TTT            87       66      24      80      83
TTT            71       60      64      52      90
TTT            60       49      31      23      99
TTT            94       11      25      24      51
TTT            15       13      39      67      97
TTT            19       76      12      33      99
TTT            18       92      35      74      0
TTT            95       71      39      33      39
TTT            32       37      45      57      71

--------------------------------
Process exited with return value 0
Press any key to continue . . .


Why is the random generation only displaying TTT ? it should be randomized letters.
Lines 50 - 62

You should use srand only once in the entire program.

What happens here: time() returns current time. It is low res timer, so program could execute thousand of lines before its value changes. You are forcebilly resetting seed with the same value in loop forcing next generated value be the same.
No i mean i want randomized letters for each string located on the left of the output. Where it says:

TTT
TTT
TTT
TTT

it should be for example:

OCC
FRE
JDK
JDK
As I said you do not give your program a chance to generate another number instead resetting it to same value.
I already said what you need to do to get rid of that behavior in the first sentense of my previous post.
okay i got you. thanks so much.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
#include <string>
#include <time.h>

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

    for (int i = 0; i < 4; i++)
    {
        std::string str = "";

        for (int j = 0; j < 3; j++)
        {
            str.insert (str.end(), 1, 65 + rand() % 26);
        }
        std::cout << str << std::endl;
    }

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