generate a random password

This is a discussion on generate a random password within the C++ Programming forums, part of the General Programming Boards category; Code: For this exercise creat a program that outputs a password consisting of sequence of 8 randum upper or lowercase ...

  1. #1
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66

    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...?
    Last edited by mabufo; 02-21-2006 at 07:40 PM.
    Oh my goodness.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    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.

  3. #3
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by MadCow257
    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.
    Last edited by mabufo; 02-21-2006 at 08:05 PM.
    Oh my goodness.

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    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. #5
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    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?
    Last edited by Ideswa; 02-22-2006 at 04:39 AM.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > int p = static_cast <double> (rand()) / RAND_MAX * MAX_NUMBER +1;
    Maybe this results in an out of range index to your array.

  7. #7
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    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";
    
    [edit]-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
    Last edited by Darryl; 02-22-2006 at 07:59 AM.

  8. #8
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    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;
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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. #10
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Thanks fellas, you're ideas have helped.
    Oh my goodness.

  11. #11
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    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?
    Oh my goodness.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    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?
    Last edited by mabufo; 02-23-2006 at 08:39 AM.
    Oh my goodness.

  14. #14
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    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. #15
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    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.
    Last edited by mabufo; 02-23-2006 at 11:55 AM.
    Oh my goodness.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  2. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  3. auto generate password
    By Coconut in forum C Programming
    Replies: 4
    Last Post: 09-29-2002, 03:55 AM
  4. Random Password Generator v1.0 - download it
    By GaPe in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 08-18-2002, 01:27 AM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21