Giter Site home page Giter Site logo

Comments (2)

icza avatar icza commented on May 27, 2024 1

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.

leopku avatar leopku commented on May 27, 2024

@icza Got it. Thanks for your reply.

from gox.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.