Review Request: Convenience KRandom methods

Ingo Klöcker kloecker at kde.org
Thu Aug 12 20:16:54 BST 2010


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
http://reviewboard.kde.org/r/4992/#review7017
-----------------------------------------------------------



trunk/KDE/kdelibs/kdecore/util/krandom.h
<http://reviewboard.kde.org/r/4992/#comment7031>

    This is wrong. Quoting the man page of rand (3):
    "The rand() function returns a pseudo-random integer in the range [0, RAND_MAX]."
    
    Note the closing ']'.



trunk/KDE/kdelibs/kdecore/util/krandom.cpp
<http://reviewboard.kde.org/r/4992/#comment7030>

    This will probably crash if you call it with min = MIN_INT and max = MAX_INT because then max-min+1 = 0 (due to an integer overflow). In fact, even smaller ranges will most likely be problematic as soon as max-min+1 > MAX_INT.
    
    Apart from the integer overflow problem this method will not return uniformly distributed random numbers. See for example http://www.cplusplus.com/reference/clibrary/cstdlib/rand/.
    
    If you want uniformly distributed random numbers in a certain integer range then use
    http://www.boost.org/doc/libs/1_43_0/doc/html/boost_random/tutorial.html#boost_random.tutorial.generating_integers_in_a_range
    instead of trying to re-invent a wheel that is much more complicated than you think.
    
    As far as I'm concerned I'm against adding such a flawed method to kdelibs.


- Ingo


On 2010-08-12 13:40:52, Sebastian Trueg wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> http://reviewboard.kde.org/r/4992/
> -----------------------------------------------------------
> 
> (Updated 2010-08-12 13:40:52)
> 
> 
> Review request for kdelibs.
> 
> 
> Summary
> -------
> 
> The KRandom namespace is quite useful. However, applications need to be full of code snippets that calculate a random number in a range. IMHO it makes perfect sense to do this once in the library. Thus, I am proposing this patch to introduce two methods providing random numbers from a range.
> Feel free to improve the implementation. :)
> 
> 
> Diffs
> -----
> 
>   trunk/KDE/kdelibs/kdecore/util/krandom.h 1162164 
>   trunk/KDE/kdelibs/kdecore/util/krandom.cpp 1162164 
> 
> Diff: http://reviewboard.kde.org/r/4992/diff
> 
> 
> Testing
> -------
> 
> 
> Thanks,
> 
> Sebastian
> 
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20100812/4d386fae/attachment.htm>


More information about the kde-core-devel mailing list