Generate a Random with probability
glozano
Member, PRO Posts: 2
Hello I have a Table with 32 values, each has a probability to occur.
How would I implement this in game salad?
Answers
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:
Value1 3:1
Value2 8:1
Value3 5:1
Value4 12:1
Now add up the probabilities, in this case, 28.
So using an integer called Rand:
BTW, welcome to the Forums.
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
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
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
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
@tatiang
Ah, OK, cheers, Tatian - understood!
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
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.
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
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
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