-
Notifications
You must be signed in to change notification settings - Fork 20.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
console: seed random number generator #2227
Conversation
Updated: Fri Feb 19 12:08:56 UTC 2016 |
Current coverage is
|
@bas-vk Please update your |
@karalabe, check, thx. |
Ah, much better, thanks :D |
f := float64(src.Int63()) / (1 << 63) | ||
if f == 1 { | ||
goto again // resample; this branch is taken O(never) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this si correct here, buy maybe I misunderstand.
src.Int63()
should return an 63 bit random integer. That is, between 0
and 2^64 - 1
. If you divide by 1 << 64
it should produce the intended results [0, 1)
. Whereas dividing by 1 << 63
actually produces quite a lot of values over 1.
Please don't build the logic to make random floats yourself. seed := make([]byte, 8)
crand.Read(seed)
r := rand.New(rand.NewSource(binary.BigEndian.Int64(seed)))
...
vm.SetRandomSource(r.Float64) @karalabe It cannot be used concurrently. |
132c9fc
to
6b0d266
Compare
I think we overlapped a bit with me editing the comment and you seeing it. |
35af4f3
to
6777531
Compare
👍 |
@fjl, we did just pushed the change as you suggested. Far cleaner solution. |
LGTM 👍 |
console: seed random number generator
* core: use finalized block as the chain freeze indicator (ethereum#28683) * core/rawdb: use max(finality, head-90k) as chain freezing threshold * core: impl multi database for block data * core: fix db inspect total size bug * core: add tips for user who use multi-database * core: adapt some cmd for multi-database * core: adapter blockBlobSidecars * core: fix freezer readHeader bug --------- Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
The otto VM uses
math/rand
as source forMath.random()
. Themath/rand
package uses by default a constant seed which generates the exact same sequence of (pseudo) random floats each time the console starts. This is unexpected behavior and brittle in the context of the console. If somewhere in the code base (e.g. one of the dependencies) resets the seed formath/rand
the otto vm will produce predictable pseudo random numbers.This PR will inject a pseudo random generator into the otto vm that is only used by the otto vm and is initialized with a new seed each time the console starts.
It required an upgrade of the otto vm package.
Fix: #2181