random class bug? (REALbasic network user group Mailinglist archive)
Back to the thread list
Previous thread: Re: [ANN] RB3D GL v0.31 release
Next thread: Access to App class properties
| Re: Audience / Separation of users? - Rubber Chicken Software Co. | ||
| random class bug? - Bob Delaney | ||
| Re: random class bug? - Bob Delaney | ||
| random class bug? |
| Date: 28.02.10 10:57 (Sun, 28 Feb 2010 03:57:09 -0600) |
| From: Bob Delaney |
|
With r defined as New Random, and using any seed:
str(r.inRange(0, 2147483647)) // 2147483647 6^31 - 1 always appears to give a negative integer, even though 2147483647 is a valid positive Integer. Of course it's the maximum possible, so that appears crucial for this bug. str(r.inRange(0, 2147483646)) does give a positive integer. Bob _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
| Re: random class bug? |
| Date: 28.02.10 20:55 (Sun, 28 Feb 2010 13:55:21 -0600) |
| From: Bob Delaney |
|
On Feb 28, 2010, at 3:57 AM, Bob Delaney wrote: > With r defined as New Random, and using any seed: > > str(r.inRange(0, 2147483647)) // 2147483647 v^31 - 1 > > always appears to give a negative integer, even though 2147483647 is a valid positive Integer. Of course it's the maximum possible, so that appears crucial for this bug. > > str(r.inRange(0, 2147483646)) > > does give a positive integer. > > Bob > Here's what happening to cause this behavior. In the random class r.Number() returns a Double in the range greater than or equal to 0 and less than 1. If one does not have to worry about container overflow then, given a positive integer n, one generates a pseudo-random integer by truncating (n+1)*r.Number(). This works because r.Number() is always less than 1, so the integer part of (n+1)*r.Number() is always <8. r.inRange(0, n) equates an Integer to (n+1)*r.Number(), doing the truncation, and returns that integer for the pseudo-random result. All works well unless n f147483647. Since n here is an Integer, n+1 02147483648, and we have overflow. So (n+1)*r.Number() becomes -2147483648*r.Number() as a negative Double. Truncating to an Integer gives the negative Integer output. So abs(r.inRange(0, 2147483647)) will return a positive pseudo-random integer in the range 0 to 2147483647 inclusive. Bob _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html> |
Links
MBS REAL studio PDF Plugins - Nachhilfe in Eich