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

# rand() % n Revisited

 P: n/a Quick rand() question: I know you're not supposed to use "rand() % 1024" for instance, because it focuses on the lower bits. However, it seems to me that given that the argument is not a power of two (or near a power of two), that this is not an issue. The upper bits will participate equally in the result with the lower. Am I missing something? Thanks! -- Rich -- Oct 23 '08 #1
15 Replies

 P: n/a On Oct 22, 6:04 pm, Rich Fife

 P: n/a Paul Hsieh

 P: n/a On Thu, 23 Oct 2008 10:46:39 -0400, lawrence.jones wrote: Paul Hsieh No C compiler'srand() that I have ever seen has, by itself, a worse effect on randomoutput than that. Then you've never seen the truely bad BSD rand() that fostered most of the paranoia about rand. With it, rand() % 1 generated the sequence 0, 1, 0, 1, 0, 1, .... That would be bad, as rand() % 1 should only ever produce 0. Oct 23 '08 #4

 P: n/a Kelsey Bjarnason Paul Hsieh >No C compiler'srand() that I have ever seen has, by itself, a worse effect on randomoutput than that. Then you've never seen the truely bad BSD rand() that fostered most ofthe paranoia about rand. With it, rand() % 1 generated the sequence 0,1, 0, 1, 0, 1, .... That would be bad, as rand() % 1 should only ever produce 0. Obviously Larry is using a font that doesn't distinguish clearly enough between '%' and '&'. Yeah, that's it. -- Keith Thompson (The_Other_Keith) ks***@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Oct 23 '08 #5

 P: n/a la************@siemens.com wrote: Paul Hsieh No C compiler's rand() that I have ever seen has, by itself, aworse effect on random output than that. Then you've never seen the truely bad BSD rand() that fostered most of the paranoia about rand. With it, rand() % 1 generated the sequence 0, 1, 0, 1, 0, 1, .... FYI the value of "rand() % 1" is identically 0. Except it may be considerably slower than just writing "0". -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: Try the download section. Oct 23 '08 #6

 P: n/a Keith Thompson On Thu, 23 Oct 2008 10:46:39 -0400, lawrence.jones wrote: >>Paul Hsieh

 P: n/a CBFalconer wrote: la************@siemens.com wrote: >Paul Hsieh >No C compiler's rand() that I have ever seen has, by itself, aworse effect on random output than that. Then you've never seen the truely bad BSD rand() that fosteredmost of the paranoia about rand. With it, rand() % 1 generatedthe sequence 0, 1, 0, 1, 0, 1, .... FYI the value of "rand() % 1" is identically 0. Except it may be considerably slower than just writing "0". Not if your compiler has a good optimizer... ;-) -- +----------------------------------------------------------------+ | Charles and Francis Richmond richmond at plano dot net | +----------------------------------------------------------------+ Oct 24 '08 #8

 P: n/a Charles Richmond la************@siemens.com wrote: >>Paul Hsieh Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Oct 24 '08 #9

 P: n/a Keith Thompson wrote: Charles Richmond CBFalconer wrote: >>la************@siemens.com wrote:Paul Hsieh

 P: n/a On Oct 22, 11:48 pm, Paul Hsieh The C.L.C. FAQ about this gives extremely misleading advice on this point and it should seriously be ignored. No. Using the advice given in the C.L.C. means that you will get reasonable results, even if rand() implementation is poor, as long as rand() produces integers that are more or less uniformly distributed in 0...RAND_MAX. If you use the "rand() % n" technique you have no such guarantee. The bias is small. Do not confuse detectablity with importance. (The use of "signifcance" in the term "statistical significance" leads many people astray). - William Hughes Oct 24 '08 #11

 P: n/a William Hughes No C compiler'srand() that I have ever seen has, by itself, a worse effect on randomoutput than that. Then you have not seen a rand() implementation that switched parity on each call. I understand such an implementation not only existed but was relatively widespread. Right you are. Here is the rand() implementation from the very influential 4.4BSD-Lite. #define RAND_MAX 0x7fffffff static u_long next = 1; int rand() { return ((next = next * 1103515245 + 12345) % ((u_long)RAND_MAX + 1)); } void srand(seed) u_int seed; { next = seed; } Oct 24 '08 #12

 P: n/a On Oct 23, 7:45*pm, William Hughes

 P: n/a Paul Hsieh