On Wed, 2003-03-05 at 10:09, David J. Pearce wrote:
> Hello,
> 
> I believe there is bug in random_number_generator when you request a 
> random number between 0 and 1. For example:
> 
As far as I can understand you are asking for a ranomized integer value
in the range [0,n) (i.e. n exclusive). If randomizing bits is what you
want, then the correct call is rgen(2), not rgen(1) (which basically
mean asking for an integer between 0 and 0 which doesn't exist so the
failure is expected). This may look counterintuitive to you, but you can
easily implement your own version which does not subtract 1 from n if
wanted.

>  > random_number_generator rgen(...);
>  > cout << rgen(1) << endl; // causes assertion failure
> 
> I encountered this problem when using random_number_generator in 
> conjuction with the random_sample_n algorithm.
> I have attached a simple patch which catches the case when the parameter 
> is 1. If anyone can tell me whether this is a reasonable fix, I would be 
> grateful.
> 
The patch fixes a case which is wrong in the first place, so my answer
would be no :)

<snip>

>     result_type operator()(argument_type n) {
> !     if(n == 1) { 
> !       return 0;
> !     } else {
> !       return uniform_int<base_type>(_rng, 0, n-1)();
> !     }
>     }
>   
Not that it matters, but the 'else' here is useless. Try avoiding
unnecessary else clauses.

Regards,
--
Tarjei

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Reply via email to