[Mod] Random API

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

[Mod] Random API

by TheFanne » Fri Aug 16, 2019 04:47

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/TheFanne/raining_nodes/)

Downloads
ContentDB: https://content.minetest.net/packages/TheFanne/random_api/
Github: https://github.com/nwdamgaard/random_api
1Y34oSgNsVfERnH2x69bEd9HBjtaictP
 

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

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

Re: [Mod] Random API

by Eran » Fri Aug 16, 2019 07:59

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: 2055
Joined: Wed Sep 17, 2014 11:21
Location: Germany
In-game: Linuxdirk

Re: [Mod] Random API

by Linuxdirk » Fri Aug 16, 2019 08:54

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.
 


Return to WIP Mods



Who is online

Users browsing this forum: No registered users and 2 guests