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.
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.
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.
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
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
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.26*rand()/RAND_MAX
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.*
i guess then (rand()/RAND_MAX)*26 should do the trick.....hehe....game of precedence
This simple. Note that you are responsible of freeing up the memory allocated and also to call srand().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; }
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)
That's a long way to write zero (well almost always zero).Originally Posted by taher84
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.*