Simpler and faster implementation of Floyd's F2#1277
Merged
dhardy merged 1 commit intorust-random:masterfrom Jan 1, 2023
Merged
Simpler and faster implementation of Floyd's F2#1277dhardy merged 1 commit intorust-random:masterfrom
dhardy merged 1 commit intorust-random:masterfrom
Conversation
Contributor
Author
|
On my machine - an M1 MacBook I get the following benchmarks: before: After: |
Contributor
Author
|
The 8% slower performance on |
dhardy
requested changes
Dec 31, 2022
Member
There was a problem hiding this comment.
Good simplification. The standard proof-by-induction of Floyd's combination algorithm can be extended to prove that the output is fully shuffled. It's a little tedious. A copy of my notes for those interested.
Proof by induction
for j in (len - amount) .. len {
let t = rng.gen_range(0..=j);
if X(t) {
choose j
} else {
choose t
}
}
Where:
m = amount
X(i) = event that i is chosen
Expect:
P(X(i)) = m / len
Standard proof by induction, using notation:
Xm(i) = event that i is chosen by (no later than) step m
Xm1(i) = event i chosen at step m - 1
Rlen = a random value from 0..=len
Case m=1 is trivial.
Assume m > 1 and correct for previous step:
Assumption: for i in 0..(len-1) { P(Xm1(i)) = (amount-1) / (len-1) }
Case: i in 0..(len-1)
Xm(i) = Xm1(i) ⊻ {Rlen=i}
P(Xm(i)) = P(Xm1(i)) + P(!Xm(i))*(1/len)
P(Xm(i)) = (m-1)/(len-1) + (len-1-m+1)/(len-1)*(1/len)
P(Xm(i)) = len*(m-1)/(len*(len-1)) + (len-1-m+1)/(len*(len-1))
P(Xm(i)) = (len*m-m)/(len*(len-1))
P(Xm(i)) = m/len
Case: i=len-1
Xm(i) = {Rlen=i} ⊻ {Rlen=k where Xm1(k)}
P(Xm(i)) = 1/len + Sum(k in 0..len){1/len * P(Xm1(k))}
P(Xm(i)) = 1/len + (len-1)/len * (m-1)/(len-1)
P(Xm(i)) = 1/len + 1/len * (m-1)
P(Xm(i)) = m/len
Ok.
Fully shuffled variant:
for j in (len - amount) .. len {
let t = rng.gen_range(0..=j);
if X(t) {
choose j
swap elts x, j
} else {
choose t
}
}
Proof by induction:
X(i,j) = event that i is chosen and appears in output at position j
Expect:
P(X(i,j)) = P(X(i)) / m = 1 / len
Assume for step m-1:
Assumption: for i in 0..(len-1), j in 0..(m-1) { P(Xm1(i,j)) = 1/(len-1) }
Case: i in 0..(len-1), j in 0..(m-1)
Xm(i,j) = if Xm1(Rlen,j) then 0 else Xm1(i,j)
Xm(i,j) = !Xm1(Rlen,j) AND Xm1(i,j)
P(Xm(i,j)) = (1 - Sum(k in 0..(len-1)){1/len * 1/(len-1)}) / (len-1)
P(Xm(i,j)) = (1 - 1/len) / (len-1)
P(Xm(i,j)) = (len - 1) / len / (len-1)
P(Xm(i,j)) = 1/len
Case: i in 0..(len-1), j=m-1: P(Xm1(i,j)) = 0
Xm(i,j) = Rlen=i
P(Xm(i,j)) = 1/len
Case: i=len-1, j=m-1:
Xm(i,j) = Rlen=i
P(Xm(i,j)) = 1/len
Case: i=len-1, j in 0..(m-1):
Xm(i,j) = Xm1(Rlen, j)
P(Xm(i,j)) = Sum(k in 0..(len-1)){1/len / (len-1) = 1/len
Ok.
The one thing that needs changing is the CHANGELOG: this is a value-breaking change. Affected API must be noted.
The previous implementation used either `Vec::insert` or a second F-Y shuffling phase to achieve fair random order. Instead, use the random numbers already drawn to achieve a fair shuffle.
Contributor
Author
|
Have added some notes on this, let me know if this is worded the way you need - thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The previous implementation used either
Vec::insertor a second F-Y shuffling phase to achieve fair random order. Instead, use the random numbers already drawn to achieve a fair shuffle.