Thread: Random chars in a certain range

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Random chars in a certain range

    I'm trying to generate 1024 random numbers, staying within the first half of the ASCII table (everything < 127) but not including anything below 33. Basically I want to stay with characters which are displayable while staying away from the ones which only "display" in a console window.

    The following works, but there is a weird... thing. I can't explain it in more detail other than the number generated is occasionally 3435973836. No matter how I twist this, that number appears no less than 100 times.

    Code:
    int main (int argc, char **argv)
    {
        char    data[1024];
        char    c = 0;
        unsigned debug_data[1024];
    
        memset (data, 0, 1024);
    
        srand ((unsigned) time (0));
    
        for (unsigned i = 0; i < 1024; i++) {
                    c = rand () / (RAND_MAX / 127 + 1);  // stay within lower 127?
                    if (! (c > 33)) continue;
                    key[i] = c;
                    debug_data[i] = (unsigned) c;
        }
    
        return 0;
    }
    I build up debug_data so I can see the ASCII values for each character it outputs. That weird random value appears randomly...

    Is there anything glaringly wrong with it?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Thanks buddy
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You might want to read Prelude's article on the subject: http://eternallyconfuzzled.com/tuts/random.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  2. random number in range
    By lgg in forum Linux Programming
    Replies: 3
    Last Post: 08-14-2005, 05:15 AM
  3. Random Numbers within a range OTHER than 1-X
    By Kaelin in forum C++ Programming
    Replies: 11
    Last Post: 02-16-2005, 11:57 AM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  5. generating random numbers within a range
    By tucky in forum C Programming
    Replies: 3
    Last Post: 09-14-2001, 12:59 PM