https://bugzilla.novell.com/show_bug.cgi?id=361973


           Summary: System.Random never returns smallest value if range is
                    negative
           Product: Mono: Class Libraries
           Version: unspecified
          Platform: x86
        OS/Version: Ubuntu
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: System
        AssignedTo: mono-bugs@lists.ximian.com
        ReportedBy: [EMAIL PROTECTED]
         QAContact: mono-bugs@lists.ximian.com
          Found By: ---


Description of Problem:

If one uses System.Random.Next(low, high) with a negative value for low, it
will never return low as the random sample.


Steps to reproduce the problem:
1. Call System.Random.Next(-1,1)
2. see that it always returns 0 (never -1 as it should do half the time).


Actual Results:
0 is returned.

Expected Results:
-1 approximately half the time, 0 approximately half the time.

How often does this happen? 
every time.

Additional Information:
The bug is actually quite easy to fix.  In System.Random there is a line in
Next(int, int) that looks like:

 int result = (int)(Sample () * diff + minValue);

The problem is that (int) rounds towards zero.  You really want Math.Floor,
which rounds towards negative infinity.  If minValue is negative and Sample()
returns a number greater than zero (which it practically always will) result
will never equal minValue.

It should be:


 int result = Math.Floor (Sample () * diff + minValue);

or cast the Sample () * diff to a uint first (since it must fit as the
difference of two ints), and the rounding towards zero in that case is correct.

 int result = (int)((uint)(Sample () * diff) + minValue);


-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.
_______________________________________________
mono-bugs maillist  -  mono-bugs@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-bugs

Reply via email to