How can I randomly choose two precise numbers?

Simple Gamer ArtsSimple Gamer Arts Member, PRO Posts: 305
edited June 2018 in Working with GS (Mac)

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

Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    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

  • Simple Gamer ArtsSimple Gamer Arts Member, PRO Posts: 305

    Thanks a lot @tatiang that works fine!

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

    Or a simpler way:

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

  • Simple Gamer ArtsSimple Gamer Arts Member, PRO Posts: 305

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

    This was genius, thanks!!! @tatiang

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

    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/

Sign In or Register to comment.