Skip to content

math/rand: Read should return consistent results, regardless of buffer size #16124

Closed
@pin

Description

@pin

Read returns byte sequence that depends on the buffer length. That's because Rand uses Int63 value to produce 7 random bytes and throws away unused bits from Int63 when done with the particular call. Next call always starts with fresh Int63 leaving "gap" from previous Read if it was not finished on 7-bytes boundary.

Therefore, you can't fix seed and read pseudo-random bytes deterministically without fixing buffer size.

Proposed fix

My proposal is to keep unused Int63 reminder in the Rand struct. It let us fix the issue and still have the same bytes from the first Read call as we have now. The performance hit is minimal also. The downside is an extra state in Rand struct.

One of the other options is to use only single byte from each Int63 call. No state in Rand but the 2x-4x performance hit.

  1. What version of Go are you using (go version)?
    go version devel +f3d5478 Fri Jun 17 19:15:29 2016 +0000 linux/amd64
  2. What operating system and processor architecture are you using (go env)?
GOARCH="amd64"
GOOS="linux"
  1. What did you do?
    This is play-adopted unit-test using testing/iotest that fails: https://play.golang.org/p/dbz-VlFPCa
  2. What did you expect to see?
    The same random bytes stream no matter read buffer size used.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions