Thread: Rand function for array of chars

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Rand function for array of chars

    Is there anyway to randomnly choose letters from an array of given chars and then continue to randomnly pick more removing the last picked one?

    Say:

    Code:
    char club[8] = {'J','Q','K','A','J','Q','K','A'};
    It picks Jack (J) and then removes it from the list and continues to randomnly pick the others.

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    What are you having problems with? Post some code, your algorithm sounds good. I'm guessing you're just having trouble with some part of the implementation.
    Don't quote me on that... ...seriously

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I think the simpleast way will be shuffle the array in the beginning, and when just read it one by one in the regular order
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    The algorithm is easy, I just don't know how to exactly use the rand function to deal with characters, i have only used it with numbers...

    say:

    ran () % 10 picks a random number from 1 to 10

    how do you get it to pick a random character that is not a number from an array?

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Well let's see, array indexes are actually number right?
    Therefore...
    Code:
    char c[] = { 'A', 'B', 'C', 'D', 'E', 'F' };
    int max_index = sizeof(c);
    int rnd_idx;
    char result;
    
    do 
    {
         rnd_idx = rand()%max_index;   /* Pick random index */
    } while( c[rnd_idx] == '\0' );
    result = c[ rnd_idx ];
    c[ rnd_idx ] = '\0';   /* Mark the character as got */
    Not the most efficient way to do it, but it will work...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Thanks, will try that.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    6
    Quote Originally Posted by xuftugulus View Post
    Not the most efficient way to do it, but it will work...
    Which is why, as someone mentioned previously, one should just pre-shuffle the array, decrement a last index, and grab the last element.

  8. #8
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    It turns out that premature optimization is the root of all evil.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  2. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  3. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 12:47 PM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 02:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM