A simple, easy-to-use API for choosing random objects from a list with probabilities.
How to use
First, create a Random object using Random:new(), then add all your items using Random:add_choice(choice, probability), and finally, call Random:calc_csum().
When you want to get an item from the list, simply call Random:choose().
Use examples
It is used in my game called Raining Nodes (https://content.minetest.net/packages/T ... ing_nodes/)
Downloads
ContentDB: https://content.minetest.net/packages/T ... andom_api/
Github: https://github.com/nwdamgaard/random_api
[Mod] Random API
-
- New member
- Posts: 3
- Joined: Wed Aug 14, 2019 01:52
- GitHub: nwdamgaard
[Mod] Random API
1Y34oSgNsVfERnH2x69bEd9HBjtaictP
- Linuxdirk
- Member
- Posts: 3219
- Joined: Wed Sep 17, 2014 11:21
- In-game: Linuxdirk
- Location: Germany
- Contact:
Re: [Mod] Random API
Why not simply local myrandomvalue = mytable[math.random(#mytable)]?
Re: [Mod] Random API
That doesn't support propabilities unless you put entries with higher propablility into the table multiple times.Linuxdirk wrote:Why not simply local myrandomvalue = mytable[math.random(#mytable)]?
- Linuxdirk
- Member
- Posts: 3219
- Joined: Wed Sep 17, 2014 11:21
- In-game: Linuxdirk
- Location: Germany
- Contact:
Re: [Mod] Random API
Well, yes, that's the only downside. But in return the random selection is reduced to one single statement. So if you don't care about probabilities you're good to go.
Who is online
Users browsing this forum: No registered users and 28 guests