• 🏆 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 number

Status
Not open for further replies.
Level 4
Joined
Sep 6, 2012
Messages
88
Hi guys,

I'm sorry for asking so many questions, but can you help me with this one ? Let's say I want to get a random number to store in an integer variable, so I use this action

  • Set Integer_Random = (Random integer number between 0 and 4)
After I do something with the random number I get from it, I'd like to get another random number from the same set of numbers, but this time the result must be different from the last random number. This is an example:

1st try: got number 2 from the set from 0 -> 4.
2nd try: whichever number is fine from 0 -> 4, except 2 (it was picked once).

So, how can I write a trigger that can perform this task ?

Thank you very much for helping ! :)
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
  • Untitled Trigger 004
    • Events
    • Conditions
    • Actions
      • For each (Integer A) from 0 to 19, do (Actions)
        • Loop - Actions
          • Set TestVariable[(Integer A)] = (Integer A)
  • Untitled Trigger 004 Copy
    • Events
    • Conditions
    • Actions
      • -------- Lets say the number you got somehow was 9 --------
      • Set Temp_Integer = 19
      • -------- This number stands for the number of numbers remaining (numberception) so if its out of 20 and no numbers have --------
      • -------- been taking, this will be 19 (because 0 is also a slot). This is only set in this trigger to show a demonstration. --------
      • Set TestVariable[9] = TestVariable[Temp_Integer]
      • Set TestVariable[Temp_Integer] = 0
      • -------- Sets the number you selected to the last number in the pool and removes it --------
      • Set Temp_Integer = (Temp_Integer - 1)
      • -------- Sets the maximum pool to 1 less because you moved the last number of the pool to one of a used slot --------
like that
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
Does this event occurs one-time only ?
Like at the start of the game you have 4 random numbers from 1 ~ 4
Let's say you got 3 as your first number.
So, you want to eliminate the '3' from 1 ~ 4 ?
So now the set becomes 1, 2, 4 ?
And you want to repeat this until the set becomes empty ?

Also,
After I do something with the random number I get from it, I'd like to get another random number from the same set of numbers, but this time the result must be different from the last random number. This is an example:

Let's say we have 3-time test (looping).
Random number between 1 ~ 4 (4 elements in a set).

You got 3 as first try.
So now 3 will become your lastNumber
Next you got 2, so 2 != lastNumber - allow
So now, does 2 becomes your lastNumber, or 3 and 2 becomes your lastNumber ?

Because for the next comparison, which value should I use ?
2 ? 3 ? Or both ?
 
Level 29
Joined
Mar 10, 2009
Messages
5,016
put a condition that the '3' may not be recalled again, like a boolean array...
JASS:
globals
   boolean array chk
   integer counter = 0
endglobals

function random takes nothing returns nothing
   local integer ran = GetRandomInt(0,3)
   if chk[ran] then
      set chk[ran] = false
      set counter = counter + 1
      //execute functions here
      if counter==4 then
          //resets everything
          set counter = 0
          set chk[0] = true
          set chk[1] = true
          set chk[2] = true
          set chk[3] = true
      endif
   endif   
endfunction

function init takes nothing returns nothing
   set chk[0] = true
   set chk[1] = true
   set chk[2] = true
   set chk[3] = true
endfunction
 
Status
Not open for further replies.
Top