# Weighed random list

Hello,

I need to make a weighed random list of things that may happen for my game. Which is probably going to be something like a table of the things, with a number attacked to it as the likelihood of the specific thing being picked. So that there’s a good chance of a things A and B being picked from the list of random things, but a low chance of a thing C being picked instead. What’s the logic for that?

There are a few ways you can do this (and I’m sure someone else will jump in with something closer to your suggestion), but one easy way would be to use a couple of different tables, maybe a ‘table of likely events’, ‘table of unlikely events’, ‘table of impossible events’ etc. Then, you could have a check when ever you wanted a random event to maybe occur that went through the tables with decreasing likelihood. For example, say you wanted any kind of random event to happen about 20% of the time:

```Every turn: If a random chance of 1 in 5 succeeds begin; if a random chance of 4 in 5 succeeds begin; choose a random row from the table of likely events; say event entry; otherwise; if a random chance of 4 in 5 succeeds begin; choose a random row from the table of unlikely events; say event entry; otherwise; choose a random row from the table of impossible events; say event entry; end if; end if; end if. ```
And then of course, if you want more than just text printed, you can use To Say rules. Eg:

``````Table of Unlikely Events
Event
"[lions]"
"[tigers]"

To say lions:
say "A lion appears before you! Oh my!";
now the lion is in the location of the player.``````

That’s a pretty fair solution, yeah. Multiple tables works pretty well. I can add and delete entries for the tables at will during runtime, right? Which would allow me to trigger a certain event only once, for example (by deleting the row), or have certain events only happen when a specific condition is met (by adding the row)?

I wrote some code that does exactly that here. It allows much finer control over the probabilities than Joey’s approach does – which might be either good or bad, depending on whether you need fine control.

You definitely can remove rows during run time. The phrase is “blank out the whole row” (after you’ve chosen it). 15.10 of the documentation says more. If it’s possible to remove/blank out all the rows, you’ll want a line in the code checking that there are still entries in the table before you choose rows from it, otherwise you’ll risk getting run-time errors.

I was certain somebody would come up with an actual weighed random table, so congratulations goes to Victor for coming up with the goods so quickly

Aye, thanks a ton. Everything seems to be running smoothly right now. Let me ask another question when I’m at it.

I’m using VictorGijsbers’s (Thanks for the help again) ATTACK plugin, and I’m sorta curious as to how to force a combat to start. Like, let’s say something in the room the player is in turns hostile as a result of his action, such as picking up an item or whatever: if I just do it through “now x is hostile”, the actual combat itself doesn’t start until an action is performed, like the player waiting or trying to exit the room or such. How do I do it properly?

Also, I’m a little confused about replacing rules. How would I go about changing “killing X/you” to “defeating X/you” in ATTACK?