Skip to content

Commit

Permalink
fix logic change in identity list sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarak Ben Youssef committed Jun 13, 2023
1 parent 5a1bca3 commit 3c2f5bd
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions model/flow/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,34 +466,23 @@ func (il IdentityList) Sample(size uint) (IdentityList, error) {
n := uint(len(il))
dup := make([]*Identity, 0, n)
dup = append(dup, il...)
// if sample size is greater than total size, return all the elements
if n <= size {
return dup, nil
if n < size {
size = n
}
swap := func(i, j uint) {
dup[i], dup[j] = dup[j], dup[i]
}
err := rand.Samples(n, size, swap)
if err != nil {
return nil, fmt.Errorf("failed to generate randomness: %w", err)
return nil, fmt.Errorf("failed to sample identity list: %w", err)
}
return dup[:size], nil
}

// Shuffle randomly shuffles the identity list (non-deterministic),
// and returns the shuffled list without modifying the receiver.
func (il IdentityList) Shuffle() (IdentityList, error) {
n := uint(len(il))
dup := make([]*Identity, 0, n)
dup = append(dup, il...)
swap := func(i, j uint) {
dup[i], dup[j] = dup[j], dup[i]
}
err := rand.Shuffle(n, swap)
if err != nil {
return nil, fmt.Errorf("failed to generate randomness: %w", err)
}
return dup, nil
return il.Sample(uint(len(il)))
}

// SamplePct returns a random sample from the receiver identity list. The
Expand Down

0 comments on commit 3c2f5bd

Please sign in to comment.