• Check out the results of the Techtree Contest #19!
  • Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 22nd Icon Contest: Creep Abilities is now concluded, time to vote for your favourite set of icons! Click here to vote!

A simple question about Variables

Status
Not open for further replies.
Level 1
Joined
Apr 17, 2018
Messages
4
Untitled.jpg

The Question is how can i set variable randomnuber2 as :

function(randomnumber between 1 and 4) / variable(Rm)

I mean for example variable Rm is the randomnumber "3" between 1 and 4, and i want the variable randomnumber2 to be between 1 and 4 but not equal to Rm, which is number "3" in my example.
 
Last edited:
Something like this:

  • execute roll
    • Actions
      • Set Rm = (Random number between 1 and 4)
      • Trigger - Run randomroll <gen> (ignoring conditions)
  • random roll
  • Events
  • Conditions
  • Actions
    • Set randomnumber2 = (Random number between 1 and 4)
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • randomnumber2 Not Equal to Rm
      • Then - Actions
        • (Do actions)
      • Else - Actions
        • Trigger - Run randomroll <gen> (ignoring conditions)
When Rm is rolled, it runs another trigger that rolls randomnumber2. If the randomnumber2roll = Rm, repeat until it isn't, then do whatever action you want.
 
Adapting it to what Chaosy said.
  • Set a = (Random integer number between 1 and 4)
  • For each (Integer loop) from 0 to 0, do (Actions)
    • Loop - Actions
      • Set b = (Random integer number between 1 and 4)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • b Equal to a
        • Then - Actions
          • -------- b == a; try again --------
          • Set loop = -1
        • Else - Actions
 
If the range of numbers is small. make an unordered list of numbers.

RandomNumbers[0] = 1
RandomNumbers[1] = 2
RandomNumbers[2] = 3
RandomNumbers[3] = 4
RandomNumbersLength = 4

Set RandomNumberSelection = random number between 0 and (RandomNumbersLength - 1)
Set Rm = RandomNumbers[RandomNumberSelection]
Set RandomNumbersLength = (RandomNumbersLength - 1)
Set RandomNumbers[RandomNumberSelection] = RandomNumbers[RandomNumbersLength]

Say Rm is 2, a random number roll of 1 in the range 0...3, then the list will look like...

RandomNumbers[0] = 1
RandomNumbers[1] = 4
RandomNumbers[2] = 3
RandomNumbersLength = 3

The process can be repeated for randomnumber2.

Set RandomNumberSelection = random number between 0 and (RandomNumbersLength - 1)
Set randomnumber2 = RandomNumbers[RandomNumberSelection]
Set RandomNumbersLength = (RandomNumbersLength - 1)
Set RandomNumbers[RandomNumberSelection] = RandomNumbers[RandomNumbersLength]

Say randomnumber2 is 1, a random roll of 0 in the range 0...2, then the list will look like...

RandomNumbers[0] = 3
RandomNumbers[1] = 4
RandomNumbersLength = 2

This process can get another 2 unique random numbers, after which an exception case must occur as there are no more unique random numbers to choose from.

I recommend this approach over the above loop approaches as it will always execute with constant time complexity.
 
If the range of numbers is small. make an unordered list of numbers.

RandomNumbers[0] = 1
RandomNumbers[1] = 2
RandomNumbers[2] = 3
RandomNumbers[3] = 4
RandomNumbersLength = 4

Set RandomNumberSelection = random number between 0 and (RandomNumbersLength - 1)
Set Rm = RandomNumbers[RandomNumberSelection]
Set RandomNumbersLength = (RandomNumbersLength - 1)
Set RandomNumbers[RandomNumberSelection] = RandomNumbers[RandomNumbersLength]

Say Rm is 2, a random number roll of 1 in the range 0...3, then the list will look like...

RandomNumbers[0] = 1
RandomNumbers[1] = 4
RandomNumbers[2] = 3
RandomNumbersLength = 3

The process can be repeated for randomnumber2.

Set RandomNumberSelection = random number between 0 and (RandomNumbersLength - 1)
Set randomnumber2 = RandomNumbers[RandomNumberSelection]
Set RandomNumbersLength = (RandomNumbersLength - 1)
Set RandomNumbers[RandomNumberSelection] = RandomNumbers[RandomNumbersLength]

Say randomnumber2 is 1, a random roll of 0 in the range 0...2, then the list will look like...

RandomNumbers[0] = 3
RandomNumbers[1] = 4
RandomNumbersLength = 2

This process can get another 2 unique random numbers, after which an exception case must occur as there are no more unique random numbers to choose from.

I recommend this approach over the above loop approaches as it will always execute with constant time complexity.
But Rm and randomnumber 2 can be equal right ?And it is a loop right ?
Sorry its a bit hard to understand for me.Can you explain it to me why Rm and randomnumber2 wont be equal if it is so ?
 
Last edited:
But Rm and randomnumber 2 can be equal right ?
No.


Well, DSG made a list, and included the numbers from 1 to 4 in it. The idea is every time you get a number from that list, you have to remove it so that you won't get it again next time.

  • Set RandomNumbers[0] = 1
  • Set RandomNumbers[1] = 2
  • Set RandomNumbers[2] = 3
  • Set RandomNumbers[3] = 4
  • Set RandomNumbersLength = 4
As you can see, the list has 4 entries (indices from 0 to 3, and each respectively stores): 1, 2, 3, and 4.

Now, we tell a function to get a random number based on the existing indices - initially from 0 to 3.
Let's assume that we got the number 1, and that particular index holds the number 2.

  • Set RND = RandomInt(0, 3) -> 1
  • Set result = RandomNumbers[RND -> 1] -> 2
After getting our number, remove it from the list. Here comes the tricky part: the easiest way to do so is copying the last index's value, which is 4, and replace the chosen index (RND -> 1). Also, remember that "RandomNumbersLength" indicates the size of our list, we must subtract it by 1 now.

  • Set RandomNumbersLength = (RandomNumbersLength - 1) -> our list now has 3 entries.
  • -------- However, we didn't do anything yet to replace the index 1. Do it now --------
  • Set RandomNumbers[RND -> 1] = RandomNumbers[RandomNumbersLength -> 3] -> number 4
That's basically it - We got our number (result), and reduced the list size. The number 2 is not going to be picked again, because it got replaced by the number 4 (last index). After you're done with it, and need to do the same process, don't forget to reset the list to how it was before.

Reminder: RND variable should be set like this
  • Set RND = (Random integer number between 0 and (RandomNumbersLength - 1))
 
Last edited:
Can you explain it to me why Rm and randomnumber2 wont be equal if it is so ?
BloodSoul explains in more detail, but basically it is because it pulls and removes random numbers from a pool of random numbers. If you want to think of a real life equivalent, it is like how a raffle works where tickets are pulled from a hat and the same ticket cannot be pulled twice because they are removed from the hat as they are pulled.
 
O
No.


Well, DSG made a list, and included the numbers from 1 to 4 in it. The idea is every time you get a number from that list, you have to remove it so that you won't get it again next time.

  • Set RandomNumbers[0] = 1
  • Set RandomNumbers[1] = 2
  • Set RandomNumbers[2] = 3
  • Set RandomNumbers[3] = 4
  • Set RandomNumbersLength = 4
As you can see, the list has 4 entries (indices from 0 to 3, and each respectively stores): 1, 2, 3, and 4.

Now, we tell a function to get a random number based on the existing indices - initially from 0 to 3.
Let's assume that we got the number 1, and that particular index holds the number 2.

  • Set RND = RandomInt(0, 3) -> 1
  • Set result = RandomNumbers[RND -> 1] -> 2
After getting our number, remove it from the list. Here comes the tricky part: the easiest way to do so is copying the last index's value, which is 4, and replace the chosen index (RND -> 1). Also, remember that "RandomNumbersLength" indicates the size of our list, we must subtract it by 1 now.

  • Set RandomNumbersLength = (RandomNumbersLength - 1) -> our list now has 3 entries.
  • -------- However, we didn't do anything yet to replace the index 1. Do it now --------
  • Set RandomNumbers[RND -> 1] = RandomNumbers[RandomNumbersLength -> 3] -> number 4
That's basically it - We got our number (result), and reduced the list size. The number 2 is not going to be picked again, because it got replaced by the number 4 (last index). After you're done with it, and need to do the same process, don't forget to reset the list to how it was before.

Reminder: RND variable should be set like this
  • Set RND = (Random integer number between 0 and (RandomNumbersLength - 1))
Now i understand.
RandomNumbers[0] = 1
RandomNumbers[1] = 4
RandomNumbers[2] = 3
RandomNumbers[3] = 4

Since we Set RandomNumbersLength=(RandomNumbersLength-1), next time it is going to choose a random integer between [0,2].By the way RandomNumbers[3]=4 cant be selected because its not on the interval [0,2] and the integer "2" also cant be selected because we replaced RandomNumbers[1] = 2 with RandomNumbers[1] = 4.Thanks a lot now i can sleep well tonight :)
 
Last edited:
BloodSoul explains in more detail, but basically it is because it pulls and removes random numbers from a pool of random numbers. If you want to think of a real life equivalent, it is like how a raffle works where tickets are pulled from a hat and the same ticket cannot be pulled twice because they are removed from the hat as they are pulled.
Thanks a lot for the method.BloodSoul made me to understand it.
 
Status
Not open for further replies.
Back
Top