Generating passwords

Nov 26, 2009 at 7:45pm
there is this assignment we have that is due after 6 more hours later,
i'm pretty stuck, need some help...

not sure how to make use of the "srand" : (




Problem:


Write a C program to generate a 6 digit passwords. These 6 digits can either be numbers or alphabets. Please make sure you generate different passwords for every execution.
(Hint: Make use of the srand function and ASCII codes if necessary)
Nov 26, 2009 at 7:52pm
You usually initialize the random generator with the time() function so you'd often see it as something like srand(time(NULL));. If you do a search on this website, you'll find an example of it, or you could Google to learn more.

Mike
Nov 26, 2009 at 8:09pm
hope i can figure out what you're trying to tell me within short time.

i'll try ... @ @

thanks a lot still ! : )
Nov 26, 2009 at 8:17pm
You can seed the random number generator like this:
srand((unsigned)time(0));
I'll explain: you seed rand with a number; it takes an unsigned int. So you cast the return value of the time() function to an unsigned int to seed rand. Then you can use rand() like this:
int random_integer = (int)(rand() % n + 1); where n is the maximum value you want to get. The + 1 is necessary if you want it to be inclusive; e.g. it will generate random numbers up to and including n.
Personally I find this is a pretty good method for generating random numbers, and have never used the method above:
1
2
3
int lowest_value = 0, highest_value = 256, range = (highest_value - lowest_value) + 1;

int random_number = lowest_value + (int)((range * rand()) / (RAND_MAX + 1.0));


Does that help?

Oh and if you want letters; try adding 'a' (with the inverted commas) for lower case and 'A' for upper case.
That works because the ASCII value of the letter a (the number it is stored as) is almost always the same across every system. Where it isn't, however, because you used 'a' instead of it's numeric value (which tends to be 97 for lowercase, 65 for uppercase); it should work for any system that can print that character.

Hopefully I explained everything properly...
Nov 26, 2009 at 8:37pm
it helps a way lot ^ ^~

just that,
i'm not so sure about the letters : )

if i wanted A~Z and a~z,
how should i put it?




( thank you so much already ! )
Nov 26, 2009 at 11:57pm
using the ascii table you can see that A = 65 and Z = 90.

say you have a random number random_number using rand() between 65 and 90.

char letter = random_number;

I think it will auto-cast to char else you have to add your own casting (char)
Nov 27, 2009 at 8:35pm
Like this:
char letter = (char)number;
If number is 65, letter will be 'A'; and when printed, will print as 'A'.

In fact; here's an ASCII table: http://www.asciitable.com/
But it is better use the letters themselves -- it looks more obvious and also will work invariably on any C compiler. For whatever reason it is possible that hardcoding the numeric value of a letter might cause problems if the code is compiled on a different computer. That fact notwithstanding, it is also a lot clearer to others trying to help you or even yourself two weeks later what was happening.
Topic archived. No new replies allowed.