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.
Entire forum
➜ MUSHclient
➜ VBscript
➜ Random feature?
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Urthdigger
USA (3 posts) Bio
|
Date
| Tue 02 Nov 2004 07:10 AM (UTC) |
Message
| I'm trying to make it so when I use a command, it will sometimes say something, sometimes not (so it doesn't spam up my output). However, I can't find out how to generate random numbers. Can anyone help me? | Top |
|
Posted by
| Flannel
USA (1,230 posts) Bio
|
Date
| Reply #1 on Tue 02 Nov 2004 08:13 AM (UTC) Amended on Tue 02 Nov 2004 08:17 AM (UTC) by Flannel
|
Message
| Youll use a scripting language. Most likely VBScript (whichever you feel comfortable with: VBScript, JScript, Perl, Python).
Here it is in VBScript:
Randomize
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
If you wanted to note a number between 1 and 6:
Randomize
Note Int((6) * Rnd + 1)
You put that (and the rest of your script stuffs) in either the send box (with send to script) or in the subroutine.
Randomize generates a new sequence, yadda yadda. You 'need' to do it once an execution (once per session, whatever, you really dont need to do it if youre not too worried about it).
then the Rnd function return a number between 0 and 1 (0 <= Rnd < 1). |
~Flannel
Messiah of Rose
Eternity's Trials.
Clones are people two. | Top |
|
Posted by
| Poromenos
Greece (1,037 posts) Bio
|
Date
| Reply #2 on Tue 02 Nov 2004 03:08 PM (UTC) Amended on Tue 02 Nov 2004 03:10 PM (UTC) by Poromenos
|
Message
| Actually, you DO need randomize, otherwise it will produce the exact same numbers every time you start it up. Also, if you want it to do something half the time, you can use
If Rnd < 0.5 Then DoThis: Else DoThat |
Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it! | Top |
|
Posted by
| Shadowfyr
USA (1,788 posts) Bio
|
Date
| Reply #3 on Tue 02 Nov 2004 05:40 PM (UTC) |
Message
| He said that: "You 'need' to do it once an execution (once per session..."
But yeah, usually you would want to execute that when the script loads, either through the special function calls that execute when the world opens or by placing the line outside other functions (so it executes as the script itself loads. Executing it every time is redundant. | Top |
|
Posted by
| Natasi
(79 posts) Bio
|
Date
| Reply #4 on Wed 11 May 2005 12:26 PM (UTC) |
Message
| Is there a way to make it send a function based on the random number selected? Like when 1 is rolled it sends "smile", on two it sends "cackle", on four it sends "burp"...etc ?
I looked over the Plugin but didn't see anything like what is here. | Top |
|
Posted by
| Flannel
USA (1,230 posts) Bio
|
Date
| Reply #5 on Wed 11 May 2005 07:09 PM (UTC) |
Message
| If Rnd < 0.2 Then
World.Send SocialsList ( Rnd * Ubound (SocialsList))
End If
That right there does it (it's at the bottom of the RandomSocials plugin).
Actually, just this (the rest is just a 20% of the time thing):
World.Send SocialsList ( Rnd * Ubound (SocialsList))
That sends a random item from the array SocialsList.
You don't need to add 1 because the array starts at 0. |
~Flannel
Messiah of Rose
Eternity's Trials.
Clones are people two. | Top |
|
Posted by
| Natasi
(79 posts) Bio
|
Date
| Reply #6 on Thu 12 May 2005 01:13 AM (UTC) |
Message
| Well, I didn';t really want to use the array setup there, I wanted to try and keep it in the aliases/trigegrs without touching the Script for now. | Top |
|
Posted by
| Flannel
USA (1,230 posts) Bio
|
Date
| Reply #7 on Thu 12 May 2005 01:36 AM (UTC) |
Message
| You can't do it without a script, since mushclient itself doesn't have a random function.
If you just mean without a script file, that's doable too. You can do everything inside of an alias/trigger using the same syntax (you'll just have to load the array each time, or hardcode it, which means you can skip out on the 'check if it exists' bit, since you can assume it won't be).
Either that, or you can use a select case (or a bunch of if/elseifs), but that basically accomplishes the same exact thing just with a lot more writing, and more work to keep it updated.
You can do (practically) everything you can inside of a script file, inside of a trigger/alias. |
~Flannel
Messiah of Rose
Eternity's Trials.
Clones are people two. | Top |
|
Posted by
| ErockMahan
(81 posts) Bio
|
Date
| Reply #8 on Thu 11 Aug 2005 05:53 PM (UTC) |
Message
| I tried this, but nothing happened.
Here's what I sent to the script, just to see if it would work:
Randomize
Note Int((2) * Rnd + 1)
If Rnd = 1 then
laugh
If Rnd = 2 then
sigh
endif
Nothing happens at all when I run this function either. What am I doing wrong? Basically, I was hoping it would either laugh or sigh. I'll expand this to make it more useful, but until I get the basics, I won't be able to do that. | Top |
|
Posted by
| Shadowfyr
USA (1,788 posts) Bio
|
Date
| Reply #9 on Thu 11 Aug 2005 07:10 PM (UTC) |
Message
| Several problems ErockMahan. The first is your using the rnd function wrong. The second is that when you use 'send to script' your literally sending the commands to an interpreter, so what the interpreter sees is *actually*:
Randomize
Note Int((2) * Rnd + 1)
If Rnd = 1 then
call laugh
If Rnd = 2 then
call sigh
endif
<end of code implies second end if>
In other words, if Rnd ever did come up as 1 or 2, it would attempt to 'call' a function named 'laugh' or 'sigh', instead of sending it to the mud. But this never happens, because you did the random number generation wrong too. Here is literally, in more human terms, what you asked it to do.
1. Seed the generator with the current time code.
2. Generate a random number and adjust it to the 1-2 range, then display it.
3. Start an if-then.
4. Generate a random number between 0 and .9999999, then test it against 1. Fails.
5. Start a new if-then
6. Generate a random number between 0 and .9999999, then test it against 2. Fails.
7. End the 'second' if-then.
8. Script exits, so never tells you that you forgot the 'end if' for the first if-then.
Now, the solution is simple. You need to place the value you generate in the 'note' into an actual variable. Rnd is itself a function call, not a variable. Due to the fact that all script inside a 'send to script' executes in what is called the global scope, you need a unique name. By global I mean that its shared automatically by 'all' script fragments and the main script, if you have one. This means that if you have a variable called 'Fish', which lets say contains the name of a type of fish you are trying to catch, but you also use 'Fish' someplace else, like for your random number, then 'Fish' would suddenly lose the value 'Salmon' and you would find yourself trying to catch a 1 or 2. Its one of the limitations of using 'send to script', which you have to be careful of. "Never" use the same name twice, unless you know you don't need to keep the value. For another example. You might use Count, to count how many times the line, "You see a fuzzy bunny!", comes up, but if you also use Count to do:
for Count = 1 to 5
send "heal"
next
Your going to have problems. So, to fix your current problem, we need to makes some adjustments. First, before showing the note, we need to drop it in a variable, fix your math a bit, then we need to fix the if-thens, so they work right and finally, we need to tell Mushclient to 'send' the words to the mud, so the script system doesn't assume they are names for something in the script itself:
Randomize
ls_num = Int(2 * Rnd) + 1
Note ls_num
If ls_num = 1 then
send "laugh"
end if
If ls_num = 2 then
send "sigh"
end if
And that should do it.
| 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.
34,684 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top