#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Generate a Random with probability

Posts: 2Member, PRO

Hello I have a Table with 32 values, each has a probability to occur.

How would I implement this in game salad?

• I am here.Posts: 6,577Member, Sous Chef, PRO
edited July 2014

Hi @glozano Interesting problem....

I'd do this way (if my logic is correct, this solution should be a "fair" system within the parameters you're asking for – there might be a more elegant, condensed version that another member might suggest, but this "expanded" version at least shows you what's going on):

Let's make an example with a table of just 4 values to show the principle:

# Value4 12:1

Now add up the probabilities, in this case, 28.

So using an integer called Rand:

Change Attribute Rand to random(1,28)

Rule: When Rand >0 and <4
Change Attribute YourPickedValue to tableCellValue(YourTable,1,1)

Rule: When Rand >3 and <12
Change Attribute YourPickedValue to tableCellValue(YourTable,2,1)

Rule: When Rand >11 and <16
Change Attribute YourPickedValue to tableCellValue(YourTable,3,1)

Rule: When Rand >15 and <29
Change Attribute YourPickedValue to tableCellValue(YourTable,4,1)

BTW, welcome to the Forums.

""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

• Posts: 11,858Member, Sous Chef, PRO, Senior Sous-Chef

Check out the algorithm here under the comment "pretend that we have the following list."

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• I am here.Posts: 6,577Member, Sous Chef, PRO
edited July 2014

Which algorithm would that be, @tatiang? as far as I can tell, stripping away the code from the page seen via your link, the principle is the same as I've given... I'd be interested to know how the principle is different, if it is...

""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

• Posts: 11,858Member, Sous Chef, PRO, Senior Sous-Chef

Sorry, @gyroscope, I started writing a longer post much like yours but got stuck at one point and decided to Google a few things... by the time I posted mine you had already given a better response.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• I am here.Posts: 6,577Member, Sous Chef, PRO

@tatiang

Ah, OK, cheers, Tatian - understood!

""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

• Posts: 2Member, PRO

Thanks for your fast responses, It looks good, but I´m trying to make it more condensed, as you said, this would give me 32 rules
I was thinking assigning more rows to the values with more probability in a table, and then make the random over the total rows

For example
Value 1
Value 1
Value 1
Value 2
Value 2
Value 3
Value 4
Value 4
Value 4
Value 4
Value 4

But that gives me another problem, because once I have used the value, I need to delete it from the list, and this way, I would need another bunch of logic to delete the used values.

• Posts: 11,858Member, Sous Chef, PRO, Senior Sous-Chef

I agree that for 32 values you might want to use multiple rows for each value instead. It doesn't require much in the way of logic because once you've selected a row and retrieved the value for that row, you just delete the row.

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

• Posts: 2Member, PRO

Yes, but deleting the row, means there is other "N" rows with that same value that weren´t deleted, that´s the other problem I have to address now, so if anyone has a better suggestion, is welcome

• Posts: 11,858Member, Sous Chef, PRO, Senior Sous-Chef

Oh right, you'd want to delete the other rows with that value... I see. So that would require a bit more logic using tableSearch().

New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User