You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RandomIntWeight chooses an index randomly using the listed non-negative relative weights.
The default Rand of math/rand package is used for random data.
Implementation guarantees to return an integer in the range of [0..len(weights)). If no weight is provided or they add up to 0, -1 is returned. Behavior for negative weights is undefined.
So for example if you pass [2, 1], then an index either 0 or 1 will be returned, and 0 will be returned with twice the probability that of 1, because the first number you pass is double that of the second.
Similarly, if you pass [5, 3, 2], then an index either of 0, 1 or 2 will be returned; 0 will be returned with 50% chance, 1 will be returned with 30% chance and 2 will be returned with 20% chance.
The returned index is chosen randomly, and varies from call to call. Also note that you must seed the rand package properly, e.g. with rand.Seed(time.Now().UnixNano()).
Also note that the output of the Go Playground is cached, and the time on the Playground is fixed (meaning test it on your computer).
First of all, thanks for this awesome library.
Result running in playground is
1
But result of same code running in my laptop is
2
BTW, I am not exactly know when to use RandomIntWeight func
The text was updated successfully, but these errors were encountered: