# How to make an probability table using Weights?

0 favourites
• 6 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
• Hello!

I have a question about how can I do a good Probability Table using Weights, like the default one a lot of games have, as example: Minecraft Loot Tables.

I have this .txt imgur.com/a/eLmdTDp with some weighted values, 10 being the most common and 1 the rarest. The print doesn't show the total, but it's 264 possibility values.

So, I want to know a good way to choose one of these values by probability, if you know any type of solution or you can help, i would really appreciate!

#### Tagged:

• Easy fix is to move to Construct 3 lol. I guess you could add values to rows of an array and pick a random row.

• Easy fix is to move to Construct 3 lol. I guess you could add values to rows of an array and pick a random row.

Sadly this is a construct 2 How do I forum. By the way, explain me more about the array solution, how could I suit it in a way that would work with all the values of the .txt?

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• If you put all those values in an array and just get one at random that would work.

Array.at(int(random(Array.width)))

For a more deluxe and compact version you could store the values and weights of each value. The sum of all the weights should be 1.

Then to randomly pick a value based on the weights you’d:

1. Generate a random value 0-1

2. Loop over the array

3. If the current weight<value? Then subtract the weight from the value and look at the next array position.

4. If weight>=value then you’re done and the array position you’re on is the value to use.

Anyways that’s the gist of it at least.

• If you put all those values in an array and just get one at random that would work.

Array.at(int(random(Array.width)))

For a more deluxe and compact version you could store the values and weights of each value. The sum of all the weights should be 1.

Then to randomly pick a value based on the weights you’d:

1. Generate a random value 0-1

2. Loop over the array

3. If the current weight<value? Then subtract the weight from the value and look at the next array position.

4. If weight>=value then you’re done and the array position you’re on is the value to use.

Anyways that’s the gist of it at least.

I've understanded it a bit, but I think would be better if you make an .capx for me to see how to do it, the .txt is here if you need ghostbin.com/aEb1P and all of the weighted values are stored in the ♣ symbol.

Like you said, the way that I want it to be is by having the chance to get choosed by the item weight value.

By the way, thanks a lot for helping me sir, I'm really glad!

• Ultimately you can store the data any way you want. In this example I just stored it in a list of number,name;...

Mainly because it was simple to parse.

dropbox.com/s/ly7rfnnbg5c79m8/weighted_random.capx

• 6 posts