• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Trigger] random numbers

Status
Not open for further replies.
You need a boolean array variable to indicate if number[#] has already been selected.
Then you can use a simple code to get a non-selected number:

  • Custom script: loop
  • Set RandomInteger = Random number between 1 and 4
  • Custom script: exitwhen not udg_Selected[udg_RandomInteger]
  • Custom script: endloop
  • Set Selected[tmpInteger] = true
This loop will continue until Selected[tmpInteger] is false, and select a random number for tmpInteger every execution.
After a number is successfuly selected, the number will be marked as selected. That is, Selected[tmpInteger] will be set to true.

Note that if all the numbers have already been selected it will get into an inifine loop and halt the trigger, and possibly cause lags if too many infinite loops are created...
 
Level 18
Joined
Aug 23, 2008
Messages
2,319
I believe exitwhen scripts will end the entire trigger. You'll use If/Then/Else triggers with a RANDOM variable, so add as Then - Action 'Set PreviousNumber = x', where x is the same number as the number being picked. This will get you a trigger like:

  • Actions
    • Set RANDOM = Random number between 1 and 4
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • RANDOM equal to 1
      • Then - Actions
        • <YOUR ACTOINS>
        • Set PreviousNumber = 1
      • Else - Actions
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • RANDOM equal to 2
          • Then - Actions
            • <YOUR ACTOINS>
            • Set PreviousNumber = 2
          • Else - Actions
            • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
              • If - Conditions
                • RANDOM equal to 3
              • Then - Actions
                • <YOUR ACTOINS>
                • Set PreviousNumber = 3
              • Else - Actions
                • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                  • If - Conditions
                    • RANDOM equal to 4
                  • Then - Actions
                    • <YOUR ACTOINS>
                    • Set PreviousNumber = 4
                  • Else - Actions
Then to prevent it from doing the previous number again you check if the number from RANDOM is greater then or equal to PreviousNumber. If that's so, you set RANDOM = RANDOM + 1, so it uses the actions of the next number. If that number would be 5 (which would be the case if 4 was picked before), you set RANDOM to 1 and do those actions.
 
Level 12
Joined
Mar 16, 2006
Messages
992
I believe exitwhen scripts will end the entire trigger. You'll use If/Then/Else triggers with a RANDOM variable, so add as Then - Action 'Set PreviousNumber = x', where x is the same number as the number being picked. This will get you a trigger like:

  • Actions
    • Set RANDOM = Random number between 1 and 4
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • RANDOM equal to 1
      • Then - Actions
        • <YOUR ACTOINS>
        • Set PreviousNumber = 1
      • Else - Actions
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • RANDOM equal to 2
          • Then - Actions
            • <YOUR ACTOINS>
            • Set PreviousNumber = 2
          • Else - Actions
            • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
              • If - Conditions
                • RANDOM equal to 3
              • Then - Actions
                • <YOUR ACTOINS>
                • Set PreviousNumber = 3
              • Else - Actions
                • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                  • If - Conditions
                    • RANDOM equal to 4
                  • Then - Actions
                    • <YOUR ACTOINS>
                    • Set PreviousNumber = 4
                  • Else - Actions
Then to prevent it from doing the previous number again you check if the number from RANDOM is greater then or equal to PreviousNumber. If that's so, you set RANDOM = RANDOM + 1, so it uses the actions of the next number. If that number would be 5 (which would be the case if 4 was picked before), you set RANDOM to 1 and do those actions.

This would only stop it from picking the same number twice in a row.

I use an random rolling trigger in my map. You'll have to use an integer[array], an integer for the random number, and an integer for max_random. When your number is picked, replace the top integer array number with the random integer, then reduce the max by 1.

So say you have 8 total numbers to random. It chooses #5. What happens is the max_random number becomes 7, and the number at 8 swaps with the number at 5, so that the random pool is still accurate.

Do you catch my drift? To reset it, you only need to reset the integer_max and integer[array] values.
 
Level 18
Joined
Aug 23, 2008
Messages
2,319
Sorry, my mistake. I thought he asked twice in a row, but now I read it again, I see he wanted twice at all.

Another way to do like you did is to just keep the arrayed integer and set the first choice to Set Integer[(8 - Integer_Max)] = RANDOM. This way, the integer variable will even show which of the random numbers got which picked ^^
 
Status
Not open for further replies.
Top