445,657 Members | 994 Online
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

# Generate a random number question

 P: n/a Another question,my teacher gave me a code for generate a random number from 1 - range, but I can't made it work, where is the problem? Thanks!! code: #include unsigned int RandomNumber(int range) { static int seed = 1; srand(seed); seed++; return (rand() % range) + 1; } ~ Let us linux ~ -----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World! -----== Over 100,000 Newsgroups - 19 Different Servers! =----- Jul 22 '05 #1
4 Replies

 P: n/a Wahoo wrote: Another question,my teacher gave me a code for generate a random number from 1 - range, but I can't made it work, where is the problem? Define: can't made it work The only thing that can be seen immediatly, is: Don't seed the random number generator more then once. In other words: call srand only once in your program. Typically this is done in main, at the very beginning. int main() { srand( time( 0 ) ); ... } If you seed the random number generator yourself all the time, you are actually doing harm to the quality of the generator. 'Randomness' is a statistical property of a *sequence* of numbers. You need to let the generator work on it's own in order to build up an undisturbed (quasi-)random sequence. -- Karl Heinz Buchegger kb******@gascad.at Jul 22 '05 #2

 P: n/a "Wahoo" wrote in message news:40********@lungfunggdn.org... Another question,my teacher gave me a code for generate a random number from 1 - range, but I can't made it work, where is the problem? Thanks!! code: #include unsigned int RandomNumber(int range) { static int seed = 1; srand(seed); seed++; return (rand() % range) + 1; } You need a better teacher, that function is rubbish. The C FAQ has a section on generating random numbers. http://www.eskimo.com/~scs/C-faq/s13.html questions 13.15 to 13.20. 13.16 and 13.17 explain why your teachers function is poor. john Jul 22 '05 #3

 P: n/a Here are some things to think about. Wahoo wrote: #include rand() and srand() aren't in math.h header but in stdlib.h. This is probably why the code doesn't even compile for you. Also check up on the "new" way of including the C header files (cmath and cstdlib). unsigned int RandomNumber(int range) { Hmmm. Just what does it mean to return an unsigned number but have a signed range? What SHOULD happen if "range" is 0? Or if it is negative? static int seed = 1; srand(seed); seed++; You set the seed for the generator every time you enter the function to one more than what it was set to the previous time. Why? You might want to read about what seeds are used for. return (rand() % range) + 1; See above and think about what will happen for 0 or negative values for "range". } Jul 22 '05 #4

 P: n/a "John Harrison" wrote in message news:<2g************@uni-berlin.de>... [ ... ] The C FAQ has a section on generating random numbers. http://www.eskimo.com/~scs/C-faq/s13.html questions 13.15 to 13.20. 13.16 and 13.17 explain why your teachers function is poor. The "solutions" presented in the FAQ provide little improvement. If rand() is at all competently written, several of the least significant bits have already been eliminated, so there's little real difference between using % and using / to clamp the output to a range. Unfortunately, both result in biased results except when RAND_MAX happens to be an even multiple of the range you're asking for. In the common case that RAND_MAX is a prime number, that's a pretty rare occurrence... Code like this can produce unbiased results: int rand_lim(int limit) { /* return a random number between 0 and limit inclusive. */ int divisor = RAND_MAX/(limit+1); int retval; do { retval = rand() / divisor; } while (retval > limit); return retval; } -- Later, Jerry. The universe is a figment of its own imagination. Jul 22 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.