What would be the right expression if you have 10 things that can happen but want to leave out #6?

LordTarantorLordTarantor Member, PRO Posts: 890
edited June 2014 in Working with GS (Mac)

Hi everybody:

I have 10 things that could happen to the actor (random 1,10) if it does this but if it does that, I want to leave #6 out of the things that may happen. What expression should I use to do this?

Comments

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

    Are you saying that you want to pick a random number from 1 to 10 but exclude 6 as a possible result?

    Usually if I need to exclude one or more values, I'll make a table with the possible values and then choose a random row (1 to tableRowCount), extract the data from that row, and then delete that row... and repeat as necessary.

    Here are some other clever solutions (I Googled random exclude gamesalad and they were the top results):

    https://forums.gamesalad.com/discussion/55877/random-number-excluding-current-number
    http://forums.gamesalad.com/discussion/60961/choosing-random-number-but-exclude-a-certain-number

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

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

    Or... make the 6th thing the 10th thing then use random(1,9)... :wink:

    ""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

    @gyroscope said:
    Or... make the 6th thing the 10th thing then use random(1,9)... :wink:

    Or that. :smiley:

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

  • LordTarantorLordTarantor Member, PRO Posts: 890
    edited June 2014

    Wow, that was great @tatiang‌.

    I have never use tables before but I got some courage and went for it and it worked!!!

    @gyroscope‌ solution was not going to work because if the actor does A= 6 will not apply but if it does B= 2 will not apply and so on.

    So I did a table with 9 rows and 10 columns leaving out in each column one of the 10 numbers.
    Then in the actor rule: if it does A = tableCellValue( game.Table_What Happen,random(1,9),1)

    then: If it does B = tableCellValue( game.Table_What Happen,random(1,9),2)

    and so on.
    Its working great!
    Thanks a lot!!!

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

    @LordTarantor said:
    Wow, that was great tatiang‌.

    I have never use tables before but I got some courage and went for it and it worked!!!

    gyroscope‌ solution was not going to work because if the actor does A= 6 will not apply but if it does B= 2 will not apply and so on.

    I really did guess your answer, @LordTarantor - it was my sense of humour! (OK, attempted sense...)

    @tatiang solution spot-on, isn't it. :smile: )


    Another way (serious this time) - for the record only - without using a table:

    3 integer attributes, Rand1 & Rand2 & FlipChoice

    Change Attribute Rand1 to random(1,5)
    Change Attribute Rand2 to random(7,10)
    Change Attribute FlipChoice to random(1,2)
    
    Rule: When FlipChoice = 1
    Change Attribute YourNumber to Rand1
    Otherwise
    Change Attribute YourNumber to Rand2
    

    That way, you can change the value ranges of Rand1 and/or Rand2 at any time (even at runtime, with more integer attributes if wanted).

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

  • LordTarantorLordTarantor Member, PRO Posts: 890

    @gyroscope‌ thats a good one too!!!
    Thanks a lot!

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

    @LordTarantor‌ You're welcome! Tables are quite powerful and actually not that intimidating.

    @gyroscope‌ Okay, back to high school Algebra... what's the probability of picking each number in your for the record example?

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

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

    @LordTarantor‌ You're welcome too. :smile:

    @tatiang said:
    @LordTarantor‌ You're welcome! Tables are quite powerful and actually not that intimidating.

    @gyroscope‌ Okay, back to high school Algebra... what's the probability of picking each number in your for the record example?

    I think I see what you're asking... so Rand1 range, any 1 number there has a 1 in 5 chance, Rand2 range any number there has a 1 in 4 chance... the final 1 in 2 choice deciding which of the 1in 5 or 1 in 4 it is....

    That's it broken down, is there a definitive answer as a single figure to the probability of the final result?

    Me, I don't know, you've stumped me there!

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

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

    Got it: despite splitting it into 2, the resulting number is still 1 in 9 probability; yes? (If I'm wrong, I'll blame it on the fact that it's only half hour away from midnight here... :wink: )

    ""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
    edited June 2014

    @gyroscope Heh! Someone smarter than me ( @RThurman) could figure it out but I asked because I noticed that there is initially a 50% chance of getting 1 through 5 (5 choices) and a 50% chance of getting 7-10 (4 choices) so I reckoned that the probability wasn't equal for all 9 choices. That's where my brain is on this busy Monday... phew!

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

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

    @tatiang said:
    gyroscope Heh! ...I asked because I noticed that there is initially a 50% chance of getting 1 through 5 (5 choices) and a 50% chance of getting 7-10 (4 choices) so I reckoned that the probability wasn't equal for all 9 choices.

    I was thinking the very same thing, wondering if that skewed the result but it was sort of short-circuiting my brain, so I gave up on that! :)

    @tatiang said:
    Someone smarter than me ( RThurman) could figure it out

    And ditto -- be interesting to see @RThurman answering... :smile:

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

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    Um... I don't understand the question!?!?

    But I think the answer must be somewhere on this page:
    http://www.math.com/tables/trig/tables.htm

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

    Stumped @RThurman. Aha! The question is: what is the probability of choosing each number from 1 to 10 (leaving out 6) assuming that first you randomly select a number from 1 to 5 and then you randomly select a number from 7 to 10 and finally you choose one of the two selected numbers. So for example, if you selected 2 and 9, then you would randomly choose 2 or 9 for the final value. I bet the probability isn't equal for all nine possible values...

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

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    Sorry about the total threadjack!

    For numbers 1-5 the probability is .2 * .5 = .1 (or 10 percent)

    For numbers 7-10 the probability is .25 * .5 = .125 (or 12.5 percent)

    I wasn't quite sure about the probability estimates. (Math isn't my thing.) So I built a Monte Carlo simulator to test it out. I used @gyroscope‌'s algorithm from above and ran it for 10,000 trials. The results were pretty darn close to the probability estimates. Yea math!

    Attached is the simulator. Give it a try and see probabilities at work!

  • SocksSocks London, UK.Member Posts: 12,822
    edited June 2014

    @LordTarantor

    @gyroscope

    @tatiang

    You can do it without attributes, tables or rules, just use:

    (random(1,9)+5)%10+1

    This will give you a random number between 1 and 10 excluding 6.

  • SocksSocks London, UK.Member Posts: 12,822

    @RThurman said:
    Attached is the simulator. Give it a try and see probabilities at work!

    Nice ! :D

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

    Thanks, @Socks. I think that was in the thread I linked to earlier but it's great to have such a simple solution. :)

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

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

    @RThurman said:
    Sorry about the total threadjack!

    For numbers 1-5 the probability is .2 * .5 = .1 (or 10 percent)

    For numbers 7-10 the probability is .25 * .5 = .125 (or 12.5 percent)

    I wasn't quite sure about the probability estimates. (Math isn't my thing.) So I built a Monte Carlo simulator to test it out. I used gyroscope‌'s algorithm from above and ran it for 10,000 trials. The results were pretty darn close to the probability estimates. Yea math!

    Attached is the simulator. Give it a try and see probabilities at work!

    Good stuff. :smile:

    @Socks said:
    LordTarantor

    gyroscope

    tatiang

    You can do it without attributes, tables or rules, just use:

    (random(1,9)+5)%10+1

    This will give you a random number between 1 and 10 excluding 6.

    Cool. :smile:


    And now, @LordTarantor , we hand your thread back to you.... (Good of us, eh?) :smiley:

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

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,880

    @Socks‌ -- that really is an elegant solution. You have shared that several times I think. Unfortunately, I seem to promptly forget it and to back to using the table method that @tatiang‌ suggested. (The table-row method seems much more concrete to me. And I can visualize what is going on a little easier.)

    @LordTarantor‌ -- thanks for the fun question!

  • SocksSocks London, UK.Member Posts: 12,822

    @tatiang said:
    Thanks, Socks. I think that was in the thread I linked to earlier but it's great to have such a simple solution. :)

    Ah ! Yes, missed your link !

  • SocksSocks London, UK.Member Posts: 12,822

    @RThurman said:
    Socks‌ -- that really is an elegant solution. You have shared that several times I think. Unfortunately, I seem to promptly forget it and to back to using the table method that tatiang‌ suggested. (The table-row method seems much more concrete to me. And I can visualize what is going on a little easier.)

    Agreed, it's not as intuitive or as visual as the table approach.

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

    @LordTarantor @RThurman @Socks

    Just thought of another solution - this time, any of the numbers has the same chance of being picked (still not as elegant as @Socks version tho' but here goes - although you could say it has its own charm :wink: and it's easy to see what's happening!)

    Change Attribute Rand to random(1,9)
    When Attribute Rand => 6
    Change Attribute Rand to Rand+1
    

    :smile:

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

  • SocksSocks London, UK.Member Posts: 12,822
    edited June 2014

    @gyroscope said:
    LordTarantor RThurman Socks

    Just thought of another solution - this time, any of the numbers has the same chance of being picked (still not as elegant as Socks version tho' but here goes)

    Change Attribute Rand to random(1,9)
    When Attribute Rand => 6
    Change Attribute Rand to Rand+1
    

    :smile:

    Nice, your approach has the added utility of easily being able to expand the range of numbers you want to avoid, you could easily adapt this to miss out - for example - 4,5 and 6.

  • LordTarantorLordTarantor Member, PRO Posts: 890

    Wow too many great suggestion, some of them are mind-blowing , at least for me. I feel my mind expanding... There is no spoon!!!

Sign In or Register to comment.