Skip to content

Commit

Permalink
Run next() for uninitialized Random values (#2321)
Browse files Browse the repository at this point in the history
The current implementations of XorOshiro128Plus and XorShift128Plus
return the sum of input seed values for the first value, which is
unsafe. Rather than telling people to discard the first value,
skip past it in the actual implementation.

This commit also fixes the documentation of current implementations
for consistency with the Random trait.

Resolves #2320
  • Loading branch information
EpicEric authored and jemc committed Nov 16, 2017
1 parent 981b472 commit 0785ebd
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/random/mt.pony
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MT is Random

fun ref next(): U64 =>
"""
A random integer in [0, 2^64 - 1]
A random integer in [0, 2^64)
"""
if _index >= _n() then
_populate()
Expand Down
3 changes: 2 additions & 1 deletion packages/random/xoroshiro.pony
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class XorOshiro128Plus is Random
"""
_x = x
_y = y
next()

fun ref next(): U64 =>
"""
A random integer in [0, 2^64 - 1]
A random integer in [0, 2^64)
"""
let x = _x
var y = _y
Expand Down
3 changes: 2 additions & 1 deletion packages/random/xorshift.pony
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class XorShift128Plus is Random
"""
_x = x
_y = y
next()

fun ref next(): U64 =>
"""
A random integer in [0, 2^64 - 1]
A random integer in [0, 2^64)
"""
var y = _x
let x = _y
Expand Down

0 comments on commit 0785ebd

Please sign in to comment.