How To Change An Integer To A Random Number But Skip One?
KillerPenguinStudios
Member Posts: 1,291
Hello all,
So I know how to give an integer a random number;
Change self.number to random(1,10)
But what I want to do is choose a number between 1 and 10 but skip 2 so it would be choosing at random; 1, 3, 4, 5, 6, 7, 8, 9, or 10 but the number 2 is not a choice it can choose.
Hope that makes sense. I am 100% sure this answer is around the forums but my wording isn't finding anything. Thanks in advance!!!
Comments
It has been mentioned quite a few times on the forums but the general idea is that you create a table with all possible values each in a separate row. And then when you choose a random row, you grab the tableCellValue for that row and then delete the row so there are no duplicates.
I just posted a demo I made: http://forums.gamesalad.com/discussion/comment/512547/#Comment_512547.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
@KillerPenguinStudios
Another way would be to use:
mod(2+random(0,8),10)+1
I want your brain sometimes @Socks
Thanks @tatiang,
that would be great but don't want to add another table as I already have a ton and this is just a simple loop that runs to choose a random number to place in an added row in a table. Don't want to keep deleting then reading them back again and again. LOL This will be helpful with other things though so thank you!
Hey @Socks,
Thanks a bunch!!! If it's not too much too ask, could you explain the numbers above and how the formula is working. I understand how it's working a little bit but really want to know how it is working exactly and so I can adjust the numbers accordingly and for other projects!
Thanks in advance and thanks again!
@KillerPenguinStudios
Given two numbers, a (the dividend) and n (the divisor), a modulo n (abbreviated as a mod n) is the remainder from the division of a by n.
For instance, the expression “2 mod 10” would evaluate to 2 because 2 divided by 10 leaves a remainder of 2 and “3 mod 10” would evaluate to 3 because 3 divided by 10 leaves a remainder of 3 and so on.
"10 mod 10” would evaluate to 0 because the division of 10 by 10 leaves a remainder of 0.
so given that info and @Socks solution
mod(2+random(0,8),10)+1
we then have e.g. the below:
mod(2,10)+1
(the left side = 2 since 2+random value of 0 = 2),
the mod function returns value 2 and then + 1 is added so the value is = 3
mod(10,10)+1
the mod function returns value 0 and then + 1 is added so the value is = 1
the highest value of 10 is produced when:
mod(9,10)+1
Should be a bit clearer for you now I hope..
@Socks idea is the best way, but if you don't like it or you want to leave out two or more numbers, and you don't want to use tables, you can use a loop.
i would just use two rules (not as elegant as @socks)
Change self.number to random(1,9)
if self.number = 2 then change self.number to 10
then run the next thing...