1. ## generate a random password

Code:
```For this exercise creat a program that outputs a password
consisting of sequence of 8 randum upper or lowercase letters or
digits. Duplicate characters are permitted.```
My question is, that I'm not sure how to get this done. I have an idea though... but I'm not sure if it is a good way to do this. My idea was to generate at random, the corresponding decimal ASCII codes for the characters, and then use a static cast to make them type char. In doing so, they will be converted to characters... (right?) Well, that's my understanding of how that works, will that idea work? Or will the static cast not produce a character? Is this a good way to do this? Or should I think of a better solution (this one is a bit complicated - considering I'll have to acount for the leading zeroes on the corresponding ASCII decimal values from 000 (null) to 099 (c). Also, I'll have to filter out characters that are not letters or numbers.

My guess, is that what I am thinking about, if even do able (think about my static cast) is too complicated for this seemingly easy problem. What do you all think? Is there an easier way to go about doing this?

EDIT: If that way is doable (which I'm not sure it is, the static cast part I'm not sure of) ... would I have to account for the leading zeroes? or...?

2. That is a way, but not the best. Create an array which holds all the numbers and letters. Then you will simply find random indexes into the array and display.

That is a way, but not the best. Create an array which holds all the numbers and letters. Then you will simply find random indexes into the array and display.
I haven't learned arrays yet...

EDIT: But there has to be an easier way than the one I suggested.

4. I can't think of anything easier then what I posted...so brute force.

Here is some starting code, you will have to add something to filter out the nonnumerical/alphabetical chars if you use it.
http://www.lookuptables.com/
Code:
```#include <iostream>
#include <rand.h>
#include <ctime>

using namespace std;

int main()
{
srand(time(NULL));
char value;
for (int i = 0; i < 8; i++)
{
value = rand() % (122 - 48 + 1) + 48;//generate random chars with values between 48 and 122
cout << value;
}
cout << "\n";
return 0;
}```

5. this could be the array way:

Code:
```#include <iostream.h>
#include <rand.h>
#include <ctime>

using namespace std;

int main()
{

srand(GetTickCount());
string posses[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",};
const int MAX_NUMBER = sizeof(posses) / sizeof(string);
for (int i = 0; i < 10; i++)
{
int p = static_cast <double> (rand()) / RAND_MAX * MAX_NUMBER +1;
cout << posses[p];
}
cout << endl;
return 0;
}```
I tested both the programs and when I executed my program a lot of times in a short period a critical error appeared and the program closed! Why?

6. > int p = static_cast <double> (rand()) / RAND_MAX * MAX_NUMBER +1;
Maybe this results in an out of range index to your array.

7. If you are doing a password, all the character are going to be char even the digits
so you have three ranges of char
numbers 0-9 = ascii 48-57
letters A-Z = ascii 65-90
letters a-z = ascii 97-122
also there would be no leading 0's in the ascii to decimal conversion but that's not needed anyway.

so...
1. generate a random_number in the range 48-122
2. if it falls into one of the gaps goto 1 and try again (gaps are 91-96 and 58-64)
3. if not output it as (char)random_number or static_cast<char>(Random_Number)
4. goto 1 and repeat until 8 numbers/letters have been output

@Ideswa, what the ???
you used string but didn't include <string> you are using deprecated header iostream.h and non-standard header rand.h

A better array (than an array of strings) would simply be an array of chars
Code:
```char foo[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

-don't know why there's a space in my string after X, but it shouldn't be there.```
however he already said he din't know arrays and he probably really doesn't know standard conatainers like strings

8. I did it like this: (It works properly now)

Code:
```void create_pass()
{
srand(time(0));
char value;
int i = 0;
while (i < 8)
{
value = rand() % (122 - 48 + 1) + 48;//generate random chars with values between 48 and 122
if (value <  65 && value > 57 || value > 90 && value < 97)
{
continue;
}
cout << value;
i++;
}
cout << endl;
}```

9. Code:
`srand(time(0));`
Move that out of your function and into main(). You only need to seed the random number generator once--not every time you call your function.

10. Thanks fellas, you're ideas have helped.

11. The thing that's confusing me is the random part of the whole thing. This is the first program that I've generated a random value with... well pseudorandom rather. Anyway, I did a little reading on random numbers and I understand about seeding the rand function with the system clock and all that - but not the actual asignment of the random value. To use Mr. Madcow's example:

Code:
` value = rand() % (122 - 48 + 1) + 48;//generate random chars with values between 48 and 122`
I understand the rand function is being called - but I don't get the algebra. I know that is supposed to set up a certain range... but how does it do that? It takes the rand function, divides it by 123 and returns the remainder... But how does that make it work in the range of 48-122?

12. Code:
`value = rand() % (122 - 48 + 1) + 48;//generate random chars with values between 48 and 122`
122 - 48 + 1 = 75
Code:
`value = rand() % 75 + 48;`
rand() % 75 results in a value from 0-74.

48 + 0 = 48
48 + 74 = 122

13. I understand a little better now, still fuzzy though!

So let me get this straight. If I wanted to get a random number - say, 1-10. I would do this:
Code:
```{
srand(time(NULL));
int n = 0;

n = rand() % 11 ;```
and if I wanted to do a specific range of numbers, say from 20-30. I would do this:
Code:
``` srand(time(NULL));
int n = 0;

n = rand() % 10 + 20 ;```
Am I understanding this correctly?

14. here is the general case: n = rand()%(size_of_range)+first_number_inrange
so...
Code:
```{
srand(time(NULL));
int n = 0;

n = rand() % 11 ;```
won't give you 1 - 10, it will give you 0 to 10. Your range size is 11, your first number in range is 0 (rand()%11 + 0)

The second will give you 20-29 (10 numbers)

15. I see... It's making more and more sense now.

EDIT:
Code:
`  n = rand()%(size_of_range)+first_number_inrange`
It makes perfect sense now.