Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Variables and math.random

Variables and math.random

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Sparafucile   (9 posts)  Bio
Date Mon 29 May 2006 01:52 AM (UTC)
Message
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!
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #1 on Mon 29 May 2006 02:35 AM (UTC)
Message
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)



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


11,510 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.