What would be the right expression if you have 10 things that can happen but want to leave out #6?
LordTarantor
Member, PRO Posts: 890
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
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
Or... make the 6th thing the 10th thing then use random(1,9)...
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
Or that.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
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!!!
I really did guess your answer, @LordTarantor - it was my sense of humour! (OK, attempted sense...)
@tatiang solution spot-on, isn't it. )
Another way (serious this time) - for the record only - without using a table:
3 integer attributes, Rand1 & Rand2 & FlipChoice
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
@gyroscope thats a good one too!!!
Thanks a lot!
@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
@LordTarantor You're welcome too.
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
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... )
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
@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
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!
And ditto -- be interesting to see @RThurman answering...
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
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
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
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!
@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.
Nice !
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
Good stuff.
Cool.
And now, @LordTarantor , we hand your thread back to you.... (Good of us, eh?)
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
@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!
Ah ! Yes, missed your link !
Agreed, it's not as intuitive or as visual as the table approach.
@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 and it's easy to see what's happening!)
""You are in a maze of twisty passages, all alike." - Zork temp domain http://spidergriffin.wix.com/alphaghostapps
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.
Wow too many great suggestion, some of them are mind-blowing , at least for me. I feel my mind expanding... There is no spoon!!!