# Random number question

Member Posts: 248
Hi could someone please share how you would get two random numbers.
In my new project I have 5 different colours,
0= pink
1= green
2= blue
3=yellow
4=orange
I'm trying to set up different combinations, I have (random) 0,4 0,3 0,2 0,1 but how would I get a random number for only (0) (3)
if I use the random expression "random 0,3" it would give me random numbers 0-3
I hope you understand what i'm asking I have used a way with tables on a previous project but there must be a simpler way
-Darren

• Member Posts: 135
Not sure if I understand what you mean, but from what you wrote first you want two random numbers ?

Make two attributes, example: Number_1 and Number_2 then make an actor do a random(0-4) on both and you'll have two different values.
• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,937
edited March 2013
Are you trying to generate a random number from the set {0,3} so that you either get a 0 or a 3? If so, then you need to do this:

Change Attribute game.random to random(0,1)
When attribute game.random=0
.....Change Attribute game.theNumber to 0
Otherwise
.....Change Attribute game.theNumber to 3

If you wanted to choose 2 or 4, you would still use random(0,1) for a 50% probability of either number:

Change Attribute game.random to random(0,1)
When attribute game.random=0
.....Change Attribute game.theNumber to 2
Otherwise
.....Change Attribute game.theNumber to 4

You're probably getting tripped up by the fact that we're using random numbers to choose other numbers. If you thought of it with something else such as animals (or colors), it would be:

Change Attribute game.random to random(0,1)
When attribute game.random=0
.....Change Attribute game.theAnimal to cow
Otherwise
.....Change Attribute game.theAnimal to horse

It doesn't really matter what the actual values are (0, 3; 2, 4; cow, horse).

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

• Member Posts: 248
@tatiang
Thats exactly what I was trying to explain.
-Darren
• Head Chef, Member, PRO Posts: 1,803
Too many rules. Change Attribute: game.random to 3*random(0,1)
• Member Posts: 334
edited March 2013
Not exactly sure what you're asking, but if you're trying to randomize those combination of numbers, check out my shuffle function below:

http://www.ezinterweb.com/2013/03/fisher-yates-in-place-shuffle-for_18.html
• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,937
Too many rules. Change Attribute: game.random to 3*random(0,1)
It's only one rule :P

Your solution works perfectly for {0,3} but as soon as they want {2,3} they have to figure out a different formula.

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

• Head Chef, Member, PRO Posts: 1,803
edited March 2013
2+random(0,1)

Or if you just want it lay it out in tables,
tableCellValue(game.TableRandom,random(1,3),1)
• Member, PRO Posts: 382
edited March 2013
@CodeMonkey
Are you only allowed to tell us brilliant things on the forums once a day?
You seem to pop up and offer suggestions no one else does.
• Head Chef, Member, PRO Posts: 1,803
edited March 2013
Yes. (Ooops, used up my limit for tomorrow)
But seriously, if I offer suggestions someone else does, then it would be a waste of my time. (And your time for reading what someone already wrote)
• Member Posts: 248
I'm still getting a bit confused guys.
Could someone please explain how I would get a random number 0-4
but missing out the 1 so
0,2,3,4. I cant seem to get my head round this lol ~X(
• Member, Sous Chef, PRO Posts: 2,864
When touch is pressed
-- Change Attribute: self.myIndex To: random(1,4)
-- When self.myIndex = 1
----Change Attribute: self.myIndex To: 0
• Member Posts: 248
@RThurman
Thanks works a treat
• I am here.Member, Sous Chef, PRO Posts: 6,577
edited April 2013

I'm still getting a bit confused guys.
Could someone please explain how I would get a random number 0-4
but missing out the 1 so
0,2,3,4. I cant seem to get my head round this lol ~X(
So if I've understood you correctly, this'll work: make two integer attributes one called Temp and another called yourNumber.

Change attribute Temp to random(1,4)
When Temp = 1
Change attribute Temp to 0
Change attribute yourNum to Temp

Edit: @RThurman beat me to it... strange, I wasn't too long and yet there's half an hour difference... whatever!

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

• Head Chef, Member, PRO Posts: 1,803
mod(random(2,5),5)

Trying to get people to use the mod(x,y) instead of %.
Otherwise it would have been random(2,5)%5
• Member, Sous Chef, PRO Posts: 2,864
@Dazza006 -- Glad it will work for you.

@CodeMonkey -- are you indicating that operators like % and/or ^ are being depreciated in favor of functions like mod(x,y) and/or pow(x)? Or are you stating a simple preference? Or are you just feeling like its just a mod(x,y) kind of day?
• Member Posts: 248
Thank you all for your help think iv got it now
• Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,937
edited April 2013
Not to belabor the point, but...

@Dazza006 If you have a bunch of these random number scenarios ("well, what about 1 to 8 but not including 2 or 5?"), you might consider using a table instead.

Just put each of your possible values in a separate row, for example:
1
3
4
6
7
8

And then do:
Change Attribute game.randomRow to random(1,tableRowCount(tableName))
.....^^ the above behavior picks a random row of the table
Change Attribute game.randomPick to tableCellValue(tableName,randomRow,1)
.....^^ the above behavior returns the value of the cell in that row (at column 1)

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

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

Good thinking from @tatiang there...

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

• Head Chef, Member, PRO Posts: 1,803
edited April 2013

@CodeMonkey -- are you indicating that operators like % and/or ^ are being depreciated in favor of functions like mod(x,y) and/or pow(x)? Or are you stating a simple preference? Or are you just feeling like its just a mod(x,y) kind of day?
% is a lua thing. Mod is not. If you start using mod now, the less likely there will be problems in lua-free situations.
^ should already have been deprecated. I think. If not, it also is a lua thing.
• Member Posts: 180
% is a lua thing. Mod is not. If you start using mod now, the less likely there will be problems in lua-free situations.
*cries*

• London, UK.Member Posts: 12,822
edited April 2013
% is a lua thing. Mod is not. If you start using mod now, the less likely there will be problems in lua-free situations.
Great tip !!

• Member, Sous Chef, PRO Posts: 2,864
edited April 2013
@CodeMonkey -- thanks for the 'heads-up'. I will start using the GS functions instead of using operators like % and ^.