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

# Random Numbers?

 P: n/a Hello, I'm trying to get a random number using C. I've tried using random(), but my program keeps producing numbers that are way out of range from the numbers I put in. Anyone got any suggestions? Kyle -- comp.lang.c.moderated - moderation address: cl**@plethora.net Nov 14 '05 #1
14 Replies

 P: n/a Kyle wrote: Hello, I'm trying to get a random number using C. I've tried using random(), but my program keeps producing numbers that are way out of range from the numbers I put in. See, for example: http://www.eskimo.com/~scs/C-faq/q13.16.html (as well as other nearby entries in the FAQ). HTH, --ag -- Artie Gold -- Austin, Texas Nov 14 '05 #2

 P: n/a On 13 Jan 2004 02:40:40 GMT, sh******@msn.com (Kyle) wrote: Hello,I'm trying to get a random number using C. I've tried using random(),but my program keeps producing numbers that are way out of range fromthe numbers I put in.Anyone got any suggestions?Kyle Use the modulus operator (%) to bring the number in range. Susan -- comp.lang.c.moderated - moderation address: cl**@plethora.net Nov 14 '05 #3

 P: n/a sh******@msn.com (Kyle) writes: I'm trying to get a random number using C. I've tried using random(), but my program keeps producing numbers that are way out of range from the numbers I put in. Anyone got any suggestions? The multiplication on line 76 of your program should be a division. And you misspelled "onomatopoeia" in the comment on line 103. I'm just guessing, of course, since you didn't post anything that would let us figure out what the actual problem might be. There is no function in standard C called random(). The standard defines a rand() function (whose implementation is typically not very good, unfortunately), so using your system's random() function might give you better results. I presume you've read the documentation for the random() function. It probably returns numbers in a wide range; you'll need to do something to scale them to the range you want. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <*> Schroedinger does Shakespeare: "To be *and* not to be" -- comp.lang.c.moderated - moderation address: cl**@plethora.net Nov 14 '05 #4

 P: n/a "Kyle" wrote in message news:cl****************@plethora.net... Hello, I'm trying to get a random number using C. I've tried using random(), but my program keeps producing numbers that are way out of range from the numbers I put in. Anyone got any suggestions? FAQ, FAQ, FAQ...! http://www.eskimo.com/~scs/C-faq/top.html Look up Q 13.16 -- comp.lang.c.moderated - moderation address: cl**@plethora.net Nov 14 '05 #5

 P: n/a In article , Kyle writesHello,I'm trying to get a random number using C. I've tried using random(),but my program keeps producing numbers that are way out of range fromthe numbers I put in.Anyone got any suggestions? Yes, try telling us exactly what it is that you want, starting with whether you need a genuinely random number (int or double) or a pseudo-random number. -- Francis Glassborow ACCU Author of 'You Can Do It!' see http://www.spellen.org/youcandoit -- comp.lang.c.moderated - moderation address: cl**@plethora.net Nov 14 '05 #6

 P: n/a In comp.lang.c.moderated Kyle wrote: # Hello, # I'm trying to get a random number using C. I've tried using random(), # but my program keeps producing numbers that are way out of range from # the numbers I put in. So you do something like calling random(34)? That is not how random() works. # Anyone got any suggestions? Read about how random works, what header declares its prototype and how to turn on all warnings of your compiler. You would have been told your error if you only had done this. Once you have done that, the other part of your question is answered in the clc FAQ. Regards, Jens -- Jens Schweikhardt http://www.schweikhardt.net/ SIGSIG -- signature too long (core dumped) -- comp.lang.c.moderated - moderation address: cl**@plethora.net Nov 14 '05 #7

 P: n/a sh******@msn.com (Kyle) writes: Hello, I'm trying to get a random number using C. I've tried using random(), but my program keeps producing numbers that are way out of range from the numbers I put in. Anyone got any suggestions? Kyle Well, first off, random() isn't a C function; it's BSD originally. The C version is rand() (which will return an int, not long int like random()). Second, neither random() nor rand() allow you to "put in" numbers. Look it up in your documentation. It will always return a value between 0 and an implementation-defined quantity, RAND_MAX. To use rand() to produce integers in the range from x to y, inclusive, you should use something like: int result = x+(int) (y*rand()/(RAND_MAX+1.0)); Make sure that you #include ; and also that you seed the generator using srand(), unless you want the same sequence of numbers every time you run. -- Micah J. Cowan mi***@cowan.name -- comp.lang.c.moderated - moderation address: cl**@plethora.net Nov 14 '05 #8

 P: n/a "Micah Cowan" wrote in message news:cl****************@plethora.net... sh******@msn.com (Kyle) writes: Well, first off, random() isn't a C function; it's BSD originally. It may not be specified as part of ISO C but it is stretching to deny it is a C function. Of course, we know what you mean but this illustrates a problem with some of the "OT" warnings we see in clc. John. Nov 14 '05 #9

 P: n/a John L wrote: "Micah Cowan" wrote sh******@msn.com (Kyle) writes: Well, first off, random() isn't a C function; it's BSD originally. It may not be specified as part of ISO C but it is stretching to deny it is a C function. Of course, we know what you mean but this illustrates a problem with some of the "OT" warnings we see in clc. It is not specified in the standard. That means that what it does and how it should be called is up to the user. To use it in this group means you should also publish all the source for random(), else it is impossible to evaluate that use. int random(int param) { return(puts("I don't feel like working today\n", stdout)); } -- Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net) Available for consulting/temporary embedded and systems. USE worldnet address! Nov 14 '05 #10

 P: n/a "CBFalconer" wrote in message news:40***************@yahoo.com... John L wrote: "Micah Cowan" wrote sh******@msn.com (Kyle) writes: Well, first off, random() isn't a C function; it's BSD originally. It may not be specified as part of ISO C but it is stretching to deny it is a C function. Of course, we know what you mean but this illustrates a problem with some of the "OT" warnings we see in clc. It is not specified in the standard. That means that what it does and how it should be called is up to the user. To use it in this group means you should also publish all the source for random(), else it is impossible to evaluate that use. int random(int param) { return(puts("I don't feel like working today\n", stdout)); } puts() doesn't take an FILE* argument. Also "puts" adds a newline so you don't need one. Lame script kiddies... when will y'all learn to use a man page or two... If you're going to be a smartarse about your comment at least be right. Tom Nov 14 '05 #11

 P: n/a Tom St Denis wrote: puts() doesn't take an FILE* argument. Also "puts" adds a newline so you don't need one. Lame script kiddies... when will y'all learn to use a man page or two... If you're going to be a smartarse about your comment at least be right. Tom, Don't be a moron, you know he meant fputs(). It's just a typo. Nov 14 '05 #12

 P: n/a Grumble wrote: Tom St Denis wrote: puts() doesn't take an FILE* argument. Also "puts" adds a newline so you don't need one. Lame script kiddies... when will y'all learn to use a man page or two... If you're going to be a smartarse about your comment at least be right. Tom, Don't be a moron, you know he meant fputs(). It's just a typo. I experimentally unplonked him this morning. 3 out of 3 messages from him have no raison d'etre. Back in the PLONK file. -- Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net) Available for consulting/temporary embedded and systems. USE worldnet address! Nov 14 '05 #13

 P: n/a Micah Cowan wrote: Well, first off, random() isn't a C function; it's BSD originally. I wouldn't say that random() isn't a C function, since it can be implemented in C. It just isn't part of the Standard C library. To use rand() to produce integers in the range from x to y, inclusive, you should use something like: int result = x+(int) (y*rand()/(RAND_MAX+1.0)); That code has three problems: 1. If overflow were not a concern, it would compute a result from x to x+y-1, inclusive, not x to y. 2. It is likely to overflow, producing undefined results. 3. The distribution is uneven for large values of (y-x). To get the range x to y, inclusive, the following approximates even distribution when r=RAND_MAX/(y-x+1) is large. int result = x+(int) ((y-x+1)*(double)rand()/(RAND_MAX+1.0)); The difference in distribution of different integers in the range x to y, is, in general, r:(r+1), assuming that the distribution or rand() is perfectly flat from 0 to RAND_MAX. To improve this, an iterative scheme can be used which only scales rand() results which are in the range 0..(floor(RAND_MAX/(y-x+1))*(y-x+1)). Other values are either discarded and rand() called again, or somehow used with a subsequent rand() result to approximate a level distribution. Thad -- comp.lang.c.moderated - moderation address: cl**@plethora.net Nov 14 '05 #14

 P: n/a In article , sh******@msn.com says... Hello, I'm trying to get a random number using C. I've tried using random(), but my program keeps producing numbers that are way out of range from the numbers I put in. Anyone got any suggestions? You've gotten a lot of suggestions. IMO, of what you've received so far, Thad Smith's post is probably the most thorough and accurate. I'll add only one detail: he alluded to code that discards values as needed to produce a flat distribution, but didn't include such code in his post. Here's some code to do that: /* return a pseudo-random number between 0 and limit inclusive. */ int rand_lim(int limit) { int divisor = RAND_MAX/(limit+1); int retval; do { retval = rand() / divisor; } while (retval > limit); return retval; } If you want to specify both a lower and an upper limit, you can call this specifying the difference between the two, and then add the result to the lower limit you've specified -- I have code for that, but it's written as a C++ function that overloads this one, so it isn't topical here. Also note that although this is written as a loop, you can usually expect the loop to execute only once per function call, and chances of it executing more than twice in a particular call are almost astronomically remote (unless rand() has massive defects). -- Later, Jerry. The universe is a figment of its own imagination. -- comp.lang.c.moderated - moderation address: cl**@plethora.net Nov 14 '05 #15

### This discussion thread is closed

Replies have been disabled for this discussion.