Generate a Random with probability

glozanoglozano 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

  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    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:

    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:

    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. :smile:

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

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    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

  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    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

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    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

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

    @tatiang

    Ah, OK, cheers, Tatian - understood! B) :)

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

  • glozanoglozano Member, PRO Posts: 2

    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.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    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

  • glozanoglozano Member, PRO Posts: 2

    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

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    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

Sign In or Register to comment.