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
➜ Random number generator in Lua.
|
Random number generator in Lua.
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Arnumor
(11 posts) Bio
|
| Date
| Wed 07 Jul 2010 11:34 PM (UTC) |
| Message
| Okay, so I want to make a script that Ic an use for either aliases or triggers that generales a randomized, five-digit number ranging between 11111 and 99999. What's the syntax for that?
I think it'd use the math.random function, but I always have trouble figuring out how to type it in Lua when I read about a functino in the manual.
I'm not a skilled programmer, so try to make it as easy for me to understand as possible, please. :) | | Top |
|
| Posted by
| Twisol
USA (2,257 posts) Bio
|
| Date
| Reply #1 on Wed 07 Jul 2010 11:39 PM (UTC) Amended on Wed 07 Jul 2010 11:40 PM (UTC) by Twisol
|
| Message
| The first thing you want to do is "seed" the random number generator. Long story short, it'll always produce the same sequence of numbers if you don't set the seed. A common and easy seed is the current time. You can do this with:
math.randomseed(os.time())
After that, you use math.random() to get a random number. For the range you want, you'd use:
math.random(11111, 99999)
As you can see, the first parameter is the lowest number, and the second is the highest number. |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | | Top |
|
| Posted by
| Arnumor
(11 posts) Bio
|
| Date
| Reply #2 on Thu 08 Jul 2010 12:47 AM (UTC) |
| Message
| Okay, but I need to know just how to use it in the script, too. I took a shot at it, but got back an error. Let's just say for simplicity's sake that I want to make my character say the randomly-generated number. What would the LUA syntax be?
Say in this case is straightforward; "say {text}", where I want {text} to be the number.
| | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #3 on Thu 08 Jul 2010 01:28 AM (UTC) |
| Message
| It is always nice to see the error message.
Anyway, something like this:
Send ("say " .. math.random(11111, 99999) )
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Twisol
USA (2,257 posts) Bio
|
| Date
| Reply #4 on Thu 08 Jul 2010 02:46 AM (UTC) Amended on Thu 08 Jul 2010 02:47 AM (UTC) by Twisol
|
| Message
| | Explanation: That takes the result of math.random, concatenates it to "say " (so you get, for example, "say 42"), and passes that new string to Send(). The '..' operator is the string concatenation operator. |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | | Top |
|
| Posted by
| Arnumor
(11 posts) Bio
|
| Date
| Reply #5 on Thu 08 Jul 2010 04:02 AM (UTC) |
| Message
| | Thanks a ton fellas, I appreciate it. I'll give it a shot and see how it works out for me. | | 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.
23,928 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top