Thread: Help generating random characters in a string

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    3

    Question Help generating random characters in a string

    I have been trying to get my code to do the following:
    Fill the first 10 indexes in string 1 & 2 with random characters
    Fill the first 20 indexes with random characters in string 3

    print all 3 strings out

    concatenate the 2nd onto the first string (then print it out)
    and a couple of other things which I have already figured out, but generating random characters has been really giving me a hard time...

    I'm not exactly sure how to fill a string with random characters.

    I can generate random strings, but I don't think that is really the same?

    Here is what my code looks like now:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main()
    {
            char str[100] = {'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'};
            char str2[100] = {'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'};
            char str3[100] = {'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'};
    
    
            float x = strcmp(str, str3);
            float abs = fabs(x);
            int random = rand() %26;
            char randomLetter = 'a' + random;
            int i;
    
    
            srand(time(NULL));
    
    
            for(i = 0, i < 10 ; i++;);
            {
            int random = rand();
            }
    
    
            printf("Str1: %s\n", str);
            printf("Str2: %s\n", str2);
            printf("Str3: %s\n", str3);
    
    
            strcat(str, str2);
            printf("New Str1: %s\n", str);
    
    
            printf("Distance: %f\n", x);
            printf("Abs Distance: %f\n", abs);
            printf("Sqrt Distance: %f\n", sqrt(abs));
    
    
    
    
            return 0;
    }

  2. #2
    Banned
    Join Date
    Jul 2022
    Posts
    112
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int array_gen(char *, int);
    
    int array_gen(char *buffer, int length) {
    
        int i = length - 1;
        int r = 0;
    
        buffer[i] = '\0';
    
        while (0 != i) {
            i = i - 1;
            r = rand() % 26;
            buffer[i] = 'a' + r;
    
        }
    
        return 0;
    }
    
    int main(int argc, const char *argv[]) {
    
        char buffer[100];
    
        array_gen(buffer, 100);
        printf("%s\n", buffer);
    
        return 0;
    }
    Last edited by kodax; 10-14-2022 at 09:09 PM.

  3. #3
    Banned
    Join Date
    Jul 2022
    Posts
    112
    I can generate random strings, but I don't think that is really the same?
    It is impossible to generate a random number, because nothing is random.

    "Spooky" quantum mechanics is quackery.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    This is not random and probably won't compile.
    Quote Originally Posted by kodax View Post
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int array_gen(char *, int);
    
    int array_gen(char *buffer, int length) {
    
        int i = length - 1;
        int r = 0;
    
        buffer[i] = '\0';
    
        while (0 != i) {
            i = i - 1;
            r = rand() % 26;
            buffer[i] = 'a' + r;
    
        }
    
        return 0;
    }
    
    int main(int argc, const char *argv[]) {
    
        char buffer[100];
    
        array_gen(buffer, 100);
        printf("%s\n", buffer);
    
        return 0;
    }

  5. #5
    Banned
    Join Date
    Jul 2022
    Posts
    112
    Code:
    #include <stdlib.h>

  6. #6
    Banned
    Join Date
    Jul 2022
    Posts
    112
    This is not random
    Because nothing is random.

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by kodax View Post
    Because nothing is random.
    Because your code is wrong... Run it 3 times and all 3 you'll get the same result.

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    kodax:

    You need to read the manpage for rand before attempting to use it! Ditto for other functions as well!

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    int array_gen(char *, int);
    
    int array_gen(char *buffer, int length) {
    
        int i = length - 1;
        int r = 0;
    
        buffer[i] = '\0';
    
        while (0 != i) {
            i = i - 1;
            r = rand() % 26;
            buffer[i] = 'a' + r;
    
        }
    
        return 0;
    }
    
    int main(void) {
    
        srand(time(NULL));
    
        char buffer[100];
    
        array_gen(buffer, 100);
        printf("%s\n", buffer);
    
        return 0;
    }
    Last edited by rstanley; 10-15-2022 at 07:45 AM.

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    rstanley,

    A question not related to the topic, but I found this interesting: How did you highlited (in red) pieces of this code using code block?

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    In the Quick Reply field, the "A" at the top of the box sets the font color:
    Help generating random characters in a string-quick-reply-png

    In case you don't see those options, in the Settings for the site, [General Settings], [Miscellaneous Options], I have [Standard Editor] set.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My string gets random extra characters
    By crimson-med in forum C Programming
    Replies: 11
    Last Post: 12-08-2015, 09:20 AM
  2. Generating random numbers...
    By naspek in forum C Programming
    Replies: 7
    Last Post: 07-29-2009, 02:00 PM
  3. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  4. seeding with numbers, generating with characters?
    By muzihc in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 11:50 PM
  5. Generating Random characters/numbers
    By milanbarosh in forum C Programming
    Replies: 5
    Last Post: 04-28-2008, 11:31 AM

Tags for this Thread