Random Generator

May 6, 2011 at 3:46pm
I looking to write a random generator that will generate a string of char (upper, lower, numeric, symbols) up to 100 characters long.

It seems that rand can only be used for numbers. Is there any C++ library that I could make use of? I welcome any other suggestion that could help me.

TIA
May 6, 2011 at 4:06pm
no need for any additional libraries - this function can be written in just a few lines using rand() and a decent understanding of ASCII

what do you have so far?
May 6, 2011 at 4:12pm
char is also a number between -128 to 127 (and unsigned 0-255):
1
2
3
4
5
6
7
8
const ushort len = 100;
std::string password;
for(ushort i = 0; i < len; ++i)
{
   char ch = Random(255/*max value*/) - 128;
   password.push_back(ch);
}
May 6, 2011 at 4:25pm
Well, letters are numbers too right? You can use rand to generate an int, you fold that number into the range 0 - 25 (or 0 - 51) and convert those to letters.
May 6, 2011 at 4:33pm
This will generate n random numbers between min and max. Just set min to the minimum ASCII value and max to the maximum asci value you want :)

A list of ASCII values can be found here... http://www.newebgroup.com/rod/newillusions/ascii.htm (look at the decimal column).

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
#include <iostream>
using namespace std;
#include <cstdlib>
#include <ctime>

int main(){
	printf("\n\n");
	printf("*******************************\n");
	printf("** Random ASCII Generator\n");
	printf("*******************************");
	
	int n = 100;
	int min = 65;//65
	int max = 122;//122
	printf("\n\nGenerating %u random numbers between %u and %u...", n, min, max);
	
	int *numbers = new int[n];
	for(int i = 0; i < n; i++)
		numbers[i] = 0;
	
	
	srand((unsigned)time(0)); 
	for(int i = 0; i < n; i++){
		int r = (rand()%(max-min)) + min;
		numbers[i] = r;
	}
	
	printf(" success.");
	
	printf("\n\nRandom numbers found:");
	for(int i = 0; i < n; i++){
		printf("\n\t%u:\t%c", i+1, numbers[i]);
	}
	printf("\n\n");
	
	
	delete numbers;
	
	return 1;
}
May 22, 2011 at 4:47am
Something like this on my side

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
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

void main()
{
srand((unsigned)time(0)); // seed randomizer

string valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-+";
int n = valid.length; // set n to be the length of valid

int m = 100; // the size of the output
char output[m + 1]; // if we want to output the char array as string, we need to have an additional element for null char ('\0')

for (int i=0; i<m; i++) // loop this m times
{
// pick a random valid character and set it as the current output character
output[i] = valid[ rand() % n ];
}

output[m] = '\0'; // set the last character to be the null char so you can print it out as a string

cout << output << endl; // this prints out the random chars
}
May 23, 2011 at 8:37am
That looks ok. One tweak you'll need:
 
int m = 100; // the size of the output 
should be
 
const int m = 100; // the size of the output 
Topic archived. No new replies allowed.