-
Notifications
You must be signed in to change notification settings - Fork 1
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
3033 fix GetRandom* #1
Conversation
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.
having a single function and passing the slice makes sense 👍 but pickRandom
seems to complicated. Check my proposal.
random.go
Outdated
return v | ||
} | ||
|
||
for i := 0; i < length; i++ { |
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.
This seems too complicated. Why not?
for l,r := from-1,from+1; l>=0||r<len(slice);l, r = l-1, r+1 {
if r >= 0 && r < len(slice) && len(slice[r]) > 0 {
return slice[r]
}
if l >= 0 && l < len(slice) && len(slice[l]) > 0 {
return slice[r]
}
}
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.
- Your approach was implemented first. BUT corrected behaviour always tries to pick random from
l
orr
. This give more “randomness”.
hmmm. sec.
random.go
Outdated
} | ||
|
||
for l, r := from-1, from+1; l >= 0 || r < len(slice); l, r = l-1, r+1 { | ||
if r < len(slice) && len(slice[r]) > 0 { |
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.
Unlikely but what happens if r
or l
overflow?
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.
@akaspin an additional check won't hurt
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.
from
can not be less or greater than length
l
can not be greater than from-1
r
can not be less than from+1
I'll make commit with tests and small improvements.
trello