Variables and math.random

Posted by Sparafucile on Mon 29 May 2006 01:52 AM — 2 posts, 12,619 views.

#0
Hi guys. I'm trying to make an alias that generates a random number and keeps it for the duration which the alias processes the send box, so basically, each time the alias is called, a random number is generated, and is used only once, and each time the alias is called again, a new random number is generated.

Basically, the alias uses a random number to pick an element from two basic tables and uses it. So, here's what I have to define the variable:

world.SetVariable("num", "[math.random (1, table.getn (prefix))]")

prefix is a table like so

local prefix =
{
"num1",
"num2",
"num3",
etc
"numlast"
}

and the number is used as such:

Send ("buddy chat " .. prefix [num] .. " %1 " .. suffix [num])

where suffix is a table of identical size as prefix. So the result would be like, if the random number were 1, {Buddy Sparafucile} (prefix element 1) text (suffix element 1)

Now, world.SetVariable("num", "[math.random (1, table.getn (prefix))]") doesnt seem to be doing the math.random bit, because when I call the alias, I get this error:

[string "Alias: "]:39: attempt to concatenate field `?' (a nil value)
stack traceback:
[string "Alias: "]:39: in main chunk

I might be doing something stupid with brackets or something, but I'm kind of stumped. Can anyone help me out? Thanks!
Australia Forum Administrator #1
You don't want to quote the calculation.


num =  "[math.random (1, table.getn (prefix))]"
print (num)


This prints: [math.random (1, table.getn (prefix))]

You want:


num =  math.random (1, table.getn (prefix))
print (num)