[Mod] Random API

Post Reply
TheFanne
New member
Posts: 3
Joined: Wed Aug 14, 2019 01:52
GitHub: nwdamgaard

[Mod] Random API

by TheFanne » Post

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
1Y34oSgNsVfERnH2x69bEd9HBjtaictP

User avatar
Linuxdirk
Member
Posts: 2712
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: [Mod] Random API

by Linuxdirk » Post

Why not simply local myrandomvalue = mytable[math.random(#mytable)]?

Eran
Member
Posts: 123
Joined: Fri May 03, 2019 16:46

Re: [Mod] Random API

by Eran » Post

Linuxdirk wrote:Why not simply local myrandomvalue = mytable[math.random(#mytable)]?
That doesn't support propabilities unless you put entries with higher propablility into the table multiple times.

User avatar
Linuxdirk
Member
Posts: 2712
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: [Mod] Random API

by Linuxdirk » Post

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests