Thread: Random tokens

  1. #1
    Unregistered
    Guest

    Question Random tokens

    Hello -
    I've tokenized a string of characters, but now I am trying to figure out how I can display the tokens randomly on the screen. Thanks for your help!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    How randomly do you need them? If it's only a matter of printing any string to the screen without worry about the same one being printed more than once then this would work fine:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    static void printRandom ( const char *c[], int size )
    {
      int i;
      for ( i = 0; i < size; i++ )
        printf ( "%s\n", c[( rand() * size ) / RAND_MAX] );
    }
    
    int main ( void )
    {
      int i;
      char a[] = "Everybody loves working with C-Strings, right?",
           copy[BUFSIZ],
           *b[BUFSIZ] = {0},
           *tok;
      srand ( (unsigned)time ( NULL ) );
      strcpy ( copy, a );
      tok = strtok ( copy, " " );
      for ( i = 0; tok != NULL; i++ ) {
        b[i] = tok;
        tok = strtok ( NULL, " " );
      }
      printRandom ( b, i );
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I don't like to use strtok because it requires a lot more care to keep error free. So here is my alternative:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    //i hate strtok....
    char **makemultistr(char *str, int *size) {
        int i, n;
        char *ptr1, *ptr2, **rtrn_str;
    
        for(ptr1 = strstr(str, " "), i = 0; ptr1 != NULL; i++, ptr1 = strstr(ptr1, " ")) {
            if(ptr1 != NULL)
                ptr1++;
            //i will hold the number of tokens after the for loop is done
        }
    
        *size = i;
        rtrn_str = (char **)malloc(i*sizeof(char *));
    
        if(rtrn_str != NULL) {
            for(ptr1 = strstr(str, " "), i = 0; ptr1 != NULL; i++, ptr1 = strstr(ptr1, " ")) {
                if(ptr1 != NULL)
                    ptr1++;
    
                ptr2 = strstr(ptr1, " ");
                n = (ptr2 != NULL)?ptr2-ptr1:strlen(ptr1);
                rtrn_str[i] = (char *)malloc(n);
                if(rtrn_str[i] != NULL)
                    strncpy(rtrn_str[i], ptr1, n);
                else
                    fprintf(stderr, "malloc() error.\r\n");
            }
        } else
            fprintf(stderr, "malloc() error.\r\n");
    
        return rtrn_str;
    }
    
    void freemultistr(char **str, int size) {
        int i;
        for(i = 0; i < size; i++)
            free(str[i];
    
        free(str);
    }
    
    int main(void) {
        char str[] = "This is much easier to do now...",
            **str2d;
        int size, i;
    
        str2d = makemultistr(str, &size);
        srand(time(NULL));   //set random seed
    
        if(str2d == NULL)
            return 1;
    
        while(1)    //infinite loop
            printf("%s\r\n", str2d[(long)(rand()%time(NULL))%size]);
    
       //if this loop weren't infinite it would be a good idea to do this
       freemultistr(str2d);  
    
        return 0;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't like to use strtok because it requires a lot more care to keep error free.
    To each their own, though you'll rarely have problems with strtok if you take the proper care in using it.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >>To each their own, though you'll rarely have problems with strtok if you take the proper care in using it.

    The other reason I don't like using it is because you have two options with strtok. Make a copy of the string that will be tokenized or allow the input string to be altered. The fact that the string is altered is the main reason that I don't like strtok. For most applications copying a string is no big deal (my function pretty much did that) but there are times when you will be dealing with a large string and copying it isn't really an option.

    I guess you are right: "To each their own."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. String tokens and random lines
    By helloamuro in forum C Programming
    Replies: 2
    Last Post: 04-20-2008, 08:40 PM
  3. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  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