#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    65
    Rep Power
    12

    Question Generate random 2K long strings


    I need to feed a function with a 2 Kb long random string for testing purposes. I don't know how to generate this....
    Has anyone of you an advise for me, plz?
    Thx, bye
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    The concept's not that difficult. Think about it:

    You have a fixed number of characters you want to use. For simplicity's sake, assign them to an array.
    Code:
    char szChars[] = "ABCDEFGHIJKLMNO"; // etc...
    You know the size of this array, either through hard-coding, or by dynamically reading it's size.

    Now, generate a random number between the bounds of this array, and pull the character from that index. Loop this 2000 times, and you have your random string.

    What part specifically is troubling you?
    Last edited by MJEggertson; February 7th, 2003 at 02:16 AM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    3
    Rep Power
    0
    Or you could do it without a character lookup table.

    /*
    include time.h, stdlib.h
    */

    char random_string[2048] = "";
    int i = 0;

    srand (time (NULL));

    while (i < sizeof (random_string))
    {
    random_string[i] = rand () % 255;
    i++;
    }


    you get the idea :).
    Last edited by xtorsyon; February 7th, 2003 at 05:49 AM.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    65
    Rep Power
    12

    Exclamation


    Originally posted by MJEggertson
    What part specifically is troubling you?
    I understand very well your 2 examples and I thank you very very much.

    What got me confused is the string declaration syntax (i have to admit my C-newbieness here :)

    I thought it was
    char *stringa;

    while you use
    char stringa[N_CHAR]

    Are those equivalent?
    Thank you since now.
    Bye
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    char *string and char string[N_CHAR] are two different things. char *string is not allocated and is not pointing to any array whereas char string[N_CHAR] is allocated N_CHAR bytes. Analyser gave an excellent explanation about pointers and arrays in here (http://forums.devshed.com/t51185/s.html). Hope this helps.

IMN logo majestic logo threadwatch logo seochat tools logo