how can i limit random(min, max) values

diegocsdiegocs Member Posts: 531
edited July 2012 in Working with GS (Mac)
for example i have at first change attribute to random(1,5), but when the game progresses some of the values are not valid anymore, for example, if the value 3 is not valid anymore, when the attribute changes to random(1,5) i wan't it to change to any vaue between 1 and 5 except for 3, is that possible?

Answers

  • JustMe74JustMe74 Member, PRO Posts: 542
    edited July 2012
    Unfortunately, we don't have if/then capability in GS (yet?)

    To do this, you could create a boolean for each number that could become invalid (for exmaple: game.Invalid3). Then, just create a rule that says when game.Invalid3 = true and your number = 3, then regenerate the random number.

  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408
    I think someone, @tshirtbooth maybe? Had a template showing you how to use tables topic random distinct numbers.

  • ericzingelerericzingeler Member Posts: 334
    edited July 2012
    JustMe74 is on the right track, but you can make it a bit easier:

    -- 4 Attributes (Types) (explained) --

    1. StartRan (Integer) (trigger to start the function)
    2. RanNumber (Integer) (where the preliminary random number is stored)
    3. RanPicks (Text) (where previously chosen random numbers are stored)
    4. MyNewRandomPick (Integer) (your final random number you need)

    -- 4 Seperate Rules --

    1. Start the choose random number function with one switch
    2. After the random number is chosen, check if the pick is unique
    3. If number is not unique, reset RanNumber
    4. If number is not unique, start the function over again

    1. If StartRan = 1 (Flip switch to Start Function)

    1a. Change Attribute RanNumber to Random(1,5) (Pick a random number 1-5 in this case)



    2. If RanNumber Not= 0 (Start next part of function after random number has been picked)

    2a. Change Attribute StartRan = 0 (Reset the choose random number switch)

    2b. If RanPicks contains "!"..(RanNumber).."!" (check if number has already been used)

    2b-b. Change attribute StartRan = 2 (If number already been used, need to restart function)

    otherwise

    2c. Change Attribute RanPicks to (RanPicks).."!"..(RanNumber).."!" (If not used, add number to picked list for later use)

    2c-a. Change attribute MyNewRandomPick = RanNumber (Store your new unique random number to be used how you want)

    2c-a. Change attribute RanNumber = 0 (Clear attribute to fully reset function)



    3. If StartRan = 2, Change Attribute RanNumber = 0 (Reset random number)



    4. If StartRan = 2 and RanNumber = 0, Constrain StartRan = 1 (Start function over)



    Notes:

    - The "!"..(RanNumber).."!" is used to make each number unique in the RanPicks attribute. This will allow you to use this function for numbers with more than one digit.

    - Do Not place Rules 1, 2, 3 or 4 inside of each other.

    - Constrain attribute must be used in rule 4


    Let me know if you have any questions. Hope this helps, thanks!
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    Just use a non repeat random number generator tshirt shows in a video and add a static variable for the number you don't want to use again the code is much simpler than above.
  • ericzingelerericzingeler Member Posts: 334
    Just use a non repeat random number generator tshirt shows in a video and add a static variable for the number you don't want to use again the code is much simpler than above.
    I think the one tshirt made was random without repeating two numbers. The function above will never repeat a number and also avoids using timers. I try to avoid using time whenever possible. But tshirt may have a solution thats different from the repeating two numbers so you may want to check it out.

  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    Yeah you can just make a rule timer instead of a timer. But you can just replace the non repeat look to with the number you want removed and it will keep looping past it.
  • ericzingelerericzingeler Member Posts: 334
    Yeah you can just make a rule timer instead of a timer. But you can just replace the non repeat look to with the number you want removed and it will keep looping past it.

    I think I get what your saying, but what if you want to list out numbers 1-1000 in random order. Does it still work?
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,922
    Yep just add each static variable you want opted out and add it to you're equation but if you wanted numbers that high I would use a table and remove rows instead much more processor friendly.
Sign In or Register to comment.