PDA

View Full Version : [Lua] Math.random Guide



mager1794
07-07-09, 01:35 AM
Math.Random Command Guide
by mager1794

Ok now i know alot of people havn't even heard of this command so here i am to explain it
using the math.random will stop you from having to make 4 functions for 2 spells...you
will have only 3 functions... but it sounds alot better if you do alot of them say you
want it to have a chance to cast fire ball frostbolt or absolutley nothing would be hard
unless you use math.random

The Function

the function for math.random is pretty simple

A & B = numbers to randomize between
ex.
A=1
B=3

it gets a random number between 1 and 3 (includes 1 and 3 in the between)


Math.Random(A,B)

its pretty simple heres a small example o fit

pUnit:Math.Random(1,2)

that will enable me to to make a random of 2 spells or chat message or both
but it would be annoying to use if (Math.Random(1,2) == 1) for every command
so we declare it as something else like this


local spruch = Math.Random(1,2)


now all we have to do is this



if (spruch == 1) then
pUnit:SendChatMessage(14, 0, "So do you kind of understand it")


if you actually pay get explained how to use this command its pretty easy and very useful
now when scripting a mob you wont be all like should i cast this every 6 secs or 4 secs or
is that to OP ....Wait ill Randomize it at 5 seconds GENIUS!!!!


An Example Script


function Arthas_Speak(pUnit, event)
local spruch = Math.Random(1,2)
if (spruch == 1) then
pUnit:SendChatMessage(12, 0, "Hi")
end
if (spruch == 2) then
pUnit:SendChatMessage(14, 0, "Hi")
end
end

function Arthas_OnEnterCombat(pUnit, event)
pUnit:SendChatMessage(12, 0, "Lets Get Randomized")
pUnit:RegisterEvent("Arthas_Speak",10000, 0)
end

RegisterUnitEvent(NPCID, 1, "Arthas_OnEnterCombat")

makis7
07-07-09, 10:12 AM
nice!;)

Alvanaar
31-08-09, 07:06 AM
Lua is case sensitive. It has to be called like so:
rand = math.random(5)or
rand = math.random(1, 5)