• 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.

Make the Random Integer Function actually be random

Truly Randomizing your Spells and Functions

By WherewolfTherewolf



Edit: I wrote this tutorial before i know about the "Fixed Random Seed" option (turn it off to remove the horrible unrandomness in testing, it should be in preferences under file in your editer), so this is kind of useless, though the game still never creates a perfect random integer, but the idea to use a player cameras x or y is a pretty good idea for getting a random integer.



Every mappers probably used the "random integer" function at least once. But is it truly random? And if it isn't, how can i evade this issue? I made this tutorial to explain how, why and how to get around a pattern issue with Blizzard's random integer function (also random point, random real, random angle...)

Ill do this in 3 parts, if you don't really care how or why, just skip the first 2. If you want to know why just keep reading.

Why?

A machine is built for one purpose, to take something, its input (ex. force), complete a process and produce an output (ex. lets say Flying Sheep). To the very nature of this process, it will always need and input and a set process to provide an output. To how this pertains to the function is found in math, take these equations:
1+1=2
x-2=0 x=2
x^2=4 x=2
2x-2=2 x=2

What is common about all of these equations? They all equal 2. There's no way to change that, 2+2=4 and that is completely and utterly true. So to its nature, math is definite. So how do you come up with a completely random value in math? You don't, math is etched in stone and cannot be altered.

So how does this pertain to warcraft at all?
Warcrafts random number generator is a formula (one I have yet to figure out).
In essence, warcraft takes its input (the max and min values you give), does its process (the formula) and gives and output (the supposedly "Random" number).
So how come it is called random?

How?


I have to say, though it has a pattern, Blizzards random int formula works well for what it is supposed to do. The formula will not give the same output twice in a sequence of random numbers unless it has been forced to (random int between 1 and 2, not many options). This is in sense pretty good for random and may appear random the first time. But to see why its not random do this:

Create a map
Remove the triggers from the init trigger.
Make it so it does this on init, Display Messege - convert integer to string - random number between 1 and 100.

1 and 100 is a large domain so you think the game would have no issue coming up with a random int.

Test it.

Now restart it. It will give you the same integer everytime you restart the map.

This can be shown with random location, random real, random angle, basically anything random.

So now that you know why it does this, heres how to get around it.

How do I get around this?


Since math is concrete, you may think you have no chance at true randomness. But that is wrong. Since you cant achieve random through the game you'll have to tap another source (I'm making this sound more complicated then it really is).

The Human mind works differently then a computer, it can create and make new. In essence, you need a biotic factor to have actual randomness.

So here's how to use this:
The game will not give you the same value twice, though it will use the sequence every time IF not interrupted.
SO, if you make the game constantly set a pointless random value every so many iterations (i would say 1/100's of a second) it will start to make a changing list.

Now when you want randomness, rely on the player. Have a player triggered event set off the random spell, trigger, etc. You can turn off the random int cycling trigger after this.

Think about it, waits the probability you'll type or cast a spell at the EXACT same time you did it the last time? There's a minuscule chance you'll achieve this. So since the integer was being randomized and the player disrupted the sequence, the game will give different outcomes.

Here's an example:
In Dotgay style, a spell that stuns for a random period of time.
================================================================
  • Random Cycling Trigger:
  • Events:
  • Periodic Time - .01 seconds
  • Conditions:
  • (none needed)
  • Actions:
  • Set (insert pointless integer variable here) = Random number between 1 and 100
Heres another way to try and make the integers more random that's influenced by spells, this would be more specific in instances where you trying to make something random at a specific time.

  • Cycle Int on Spells
  • Events:
  • A unit (any spell event)
  • Conditions:
  • Dont need any
  • Actions:
  • Set (insert pointless integer variable here) = Random number between 1 and 100
This would cycle the int upon having the player do something, since there's no chance that all players will cast spells the exact same amount of times

  • Dotgay Random Stun Spell
  • Events:
  • A unit (whatever spell event you want (begins casting, starts the effect, etc.)
  • Conditions:
  • Ability Being Cast = Dotgay gay Stunner
  • Actions:
  • Set variable, or udg, Dotgay Stun Time [index] = Random Integer (or real, whatever you prefer) between (min stun time) and (Max Stun Time)
  • Set Dotgay Stun Target (player number owner of casting unit,if you want to get technical) = Target unit of ability being cast
  • Pause Dotgay Stun Target [Inset Index]
  • Wait (start a timer if you want) Dotgay Stun Time [Index]
  • Unpause Dotgay Stun Target [Index]

In that, the target was paused for a random amount of time. Now you can add effects and stuff if you want, but look for a different tutorial for that.

This is just one example of getting around the random int pattern. There are other ways, for example cycling the random int every time a unit casts any spell (just leave conditions blank).
Since there's barely any chance people will cast spells at the exact same time, the same amount of times this would work too in cycling randoms.

When using this, you wont have to cycle a random for every type (you don't have to repeat this for real and point values).


I hope this was helpful, please comment if you find this confusing or spelling errors (I seem to make them a lot, and this was typed up in notepad so i didn't use spell check).
-Wherewolf
 
Last edited:
Level 10
Joined
Oct 2, 2005
Messages
398
I doubt that value will be trully random in every kind of events. If you have a periodic event, influenced by these random integers, it will repeat once again when the game is restarted.
But, if you rely your random integer in something more random that the player does, it will manipulate more efficiently the integer.
Like, "the factor of the coordinates X and Y of a random user-controlled player" as the base, and then you do some features to make it more practical.
 
Level 23
Joined
Nov 29, 2006
Messages
2,482
Yeah, I thoght about it, but when you are playing the game and restart, you will get the same seed.

Actually, when enabling it it doesn't... or shouln't. I had a project once where I decided to use a random unit-type spawns. If I restarted it, it was never the same. Also, the random seed is changing everytime you resave the map, as far as I have seen.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Pointless tutorial, should be recycled.

Yes, the random integer is not truely random but thats because it has to not only keep in sync with other players (return the same value) but also be generated by a computer which is incapable of random processes.

A random integer is basically a logical calculation applied to some variables who's value changes depending on certain factors.

Thus I believe "lucky chains" where a random value is constantly the same are impossiable (yet they should be possiable if the the numbers were truely random), but overall the values returned on average appear to be random. This really is not a problem as nearly nothing needs numbers to be that random.

The problem this describes makes no sense as well, on map start the values are always random, especially on multiplayer. Thus you must be one of those people who have that problem only when testing a map, and that is purly optional as you can stop it by simply disabling the one option.
 
Level 13
Joined
May 11, 2008
Messages
1,198
to original post...i think the issue with the random and map initialization was intentional...beyond that...i think it's random.

you restart so it does the same map initialization even if it was random, it will be repeated. i think to avoid that display your message a second into the game and you'll get a different number from 1-100
 
to original post...i think the issue with the random and map initialization was intentional...beyond that...i think it's random.

you restart so it does the same map initialization even if it was random, it will be repeated. i think to avoid that display your message a second into the game and you'll get a different number from 1-100

Imba bump with no sense.. Hehe...
 
Top