#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# How can I randomly choose two precise numbers?

Posts: 276Member, PRO
edited June 2018

I want to randomly choose just these two numbers: -100 and 100 (not -99 or 56). Any ideas?

• Posts: 11,719Member, Sous Chef, PRO, Senior Sous-Chef
edited June 2018

Create two integer attributes called game.whichOne and game.randomNumber

[repeat with a loop, Timer, rule condition, etc.]
Change attribute game.whichOne to 0
Change attribute game.whichOne to random(1,2)
When attribute game.whichOne = 1
Change attribute game.randomNumber to -100
When attribute game.whichOne = 2
Change attribute game.randomNumber to 100

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

• Posts: 276Member, PRO

Thanks a lot @tatiang that works fine!

• Posts: 11,719Member, Sous Chef, PRO, Senior Sous-Chef
edited June 2018

Or a simpler way:

Change attribute game.randomNumber to (random(0,1) * 2 - 1) * 100

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

• Posts: 276Member, PRO

Change attribute game.randomNumber to (random(0,1) * 2 - 1) * 100

This was genius, thanks!!! @tatiang

• Posts: 11,719Member, Sous Chef, PRO, Senior Sous-Chef

You're welcome!

Wish I could take credit. I Googled Lua random negative one or one since GameSalad is build in Lua. The first result was this:

https://forums.coronalabs.com/topic/35410-tip-randomly-return-1-or-1/

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