Comments (2)
Please read the doc of the function: https://pkg.go.dev/github.com/icza/gox/mathx/randx#RandomIntWeight
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).
from gox.
@icza Got it. Thanks for your reply.
from gox.
Related Issues (7)
- Licensing and Attribution HOT 1
- An Apache 2.0 licence but no copyright information HOT 3
- Tests fail to run on 32-bit architectures HOT 1
- Unexpected mathx.Round() result when rounding to 2 decimal places HOT 5
- format does not work on 28th of February HOT 5
- error with opening link in windows using `func OpenDefault(fileOrURL string) error`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gox.