home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,525 of 33,346   
   alessandro.suglia@googlemail.com to All   
   Re: nrand() from Accelerated C++   
   05 Sep 12 04:21:42   
   
   { Reformatted; please limit your lines to 70 characters -mod }   
      
   Il giorno venerd́ 19 marzo 2004 23:00:15 UTC+1, Seungbeom Kim ha   
   scritto:   
      
   > This is the nrand() function from Accelerated C++:   
   >   
   > // return a random integer in the range `[0,' `n)'   
   > int nrand(int n)   
   > {   
   > 	if (n <= 0 || n > RAND_MAX)   
   > 		throw domain_error("Argument to nrand is out of range");   
   >   
   > 	const int bucket_size = RAND_MAX / n;   
   > 	int r;   
   >   
   > 	do r = rand() / bucket_size;   
   > 	while (r >= n);   
   >   
   > 	return r;   
   > }   
   >   
   > There's one thing that strikes me as odd; why is bucket_size defined   
   > as RAND_MAX/n, not (RAND_MAX+1)/n?   
   >   
   > I guess that, if RAND_MAX is 999 and n is 100, there are 1000   
   > possible output of rand(), so bucket_size had better be   
   > 1000/100=10. This does not affect the correctness, but it may cause   
   > more iterations.   
   >   
   > In addition, consider the case where n is equal to RAND_MAX+1:   
   > theoretically there should be no problem since we can use the result   
   > of rand() directly. However, the above function rejects such a case,   
   > since otherwise the divisor bucket_size would be zero. Now let   
   > bucket_size defined as (RAND_MAX+1)/n, then bucket_size would be 1   
   > and r=rand()/1 would never be >=n, so it would be returned directly   
   > with no problem.   
   >   
   > Any comments?   
      
   { Quoted signature and banner removed -mod }   
      
   Hi there,   
      
   I'm reading in these days that chapter of the book, and I've found   
   this example quite astonishgly and also quite difficult to understand   
   simply because trying to execute your program I'll always receive a   
   "random value" that is absolutely constant.   
      
   I don't know why exactly, maybe it's a problem of my computer, but   
   I've noticed that removing the division operator and substituting that   
   one with the remainder operator, the result starting to be correct.   
      
   So my question is, there are some errors in my book or it's simply a   
   distraction error??   
      
   This is the code that I'm trying to compile with Code::Blocks 10.05   
      
   Thanks for your help,   
   Alessandro Suglia   
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca