Thread: generating random words

  1. #16
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    It would be used for the same thing you are doing... I have also used it when reading in binary data so that I could check the number before trying to change it to a char.

  2. #17
    Registered User
    Join Date
    Jul 2006
    Posts
    111
    oh ok. This is one of the reasons i started to teach myself C. There are so many ways to do things that you are not bound to rigid inflexibility.

  3. #18
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    something i would like to add to add abt srand is that using srand and subsequently using rand gives you any random no upto RAND_MAX defined in stdlib(not sure check it out for urself).....so if u want to generate and random no from 1 to 26 do the operation 26*rand()/RAND_MAX and read it as int...before that add 0.5 to the no for rounding off else u would always miss 26 hence 'z'....use this random no to access ur array to generate random alphabets
    If that was of any little help

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    taher84, you need to read this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    SURELY...i would give a reading to it...thanks for it..anyway i would like to know if there is some anomaly with my method coz i have been successfully using it

  6. #21
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    26*rand()/RAND_MAX
    If RAND_MAX is the same as INT_MAX and rand returns a value close to RAND_MAX and you multiply it by 26 you've just overflowed the int range. Oops. Bad.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #22
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    i guess then (rand()/RAND_MAX)*26 should do the trick.....hehe....game of precedence

  8. #23
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    char* random_word(int n) {
      char* array = (char*)malloc(sizeof(char) * (n + 1));
      for(int i = 0; i < n; i++)
        /* 96 corresponds to 'a' ASCII */
        array[i] = (rand() % 96) + 26;
      return array;
    }
    This simple. Note that you are responsible of freeing up the memory allocated and also to call srand().

  9. #24
    Registered User
    Join Date
    Jul 2006
    Posts
    7
    mr desolation you are again missing the point that rand()%N never gives you a sufficient randomization its because the lower bits are not that random(refer to the tute above)

  10. #25
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by taher84
    i guess then (rand()/RAND_MAX)*26 should do the trick.....hehe....game of precedence
    That's a long way to write zero (well almost always zero).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  3. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. Replies: 1
    Last Post: 12-14-2002, 01:51 AM