mIRC Home    About    Download    Register    News    Help

Print Thread
#28330 05/06/03 05:14 PM
Joined: Jun 2003
Posts: 12
W
Pikka bird
OP Offline
Pikka bird
W
Joined: Jun 2003
Posts: 12
In an if statement... is there a way to script it on so only this will have a 10% chance of happening? Like say there is only a 10% chance of /msg #Chat You just won 1000 dollars!

Could you help me with this? Thanks.
Webmonkey

#28331 05/06/03 05:48 PM
Joined: Dec 2002
Posts: 3,138
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 3,138
if ( $rand(1,10) == 1 ) { commands }

#28332 05/06/03 05:55 PM
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Well the answer should be to use $rand() to generate a random number, however, mIRC's $rand() is bugged in that it will only return the first and last possibilities half as often as the others. This can be resolved with a simple alias like so:
Code:
alias my_rand {
  if ($+($1,$2) !isnum) return
  var %r = $rand($1,$calc($2 + 1))
  if (%r == $1) || (%r == $calc($2 + 1)) return $1
  return %r
}


Then you just use something like if ($my_rand(1,10) == 1) msg #Chat You just won 1000 dollars!

Note: When the next version of mIRC comes out you'll have to check whether this bug has been fixed (by looking at the versions.txt that comes with it), and if it has then switch from $my_rand() to $rand() - otherwise the first and last possibilities will end up occuring twice as often shocked.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
#28333 05/06/03 06:04 PM
Joined: Dec 2002
Posts: 2,809
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 2,809
It's not really that mIRC has bugs, it's $rand is a "PSEUDO-random number generator" there is no such thing as true random numbers, at least not without a special "random number generator" PCI card, which I assume 99% of computers don't have. Even the best PRNGs don't provide perfect statistics on the probability of a given number coming out. Granted, mIRC's $rand doesn't use the best algorithm in existence, but even the best ones wouldn't be exactly 10% for each of 10 numbers.

#28334 05/06/03 08:34 PM
Joined: Jun 2003
Posts: 12
W
Pikka bird
OP Offline
Pikka bird
W
Joined: Jun 2003
Posts: 12
That doesn't help me a lot. I mean in an if statement...

such as this for example

on *:Text:-slash Goblin:#:{
if ($nick == %Player1) { /msg $channel $nick slashes Goblin to death. | /msg $chan Does $nick pick up Goblin's backpack? (-pickup) | /set %Choice $nick }

on *:Text:-pickup:#:{
if (%choice == $nick) { /msg $chan $nick chooses to pick up the backpack and finds: a knife, 20 gold, _____ <--- this is where I want the 3% chance of there being another item in this place... I want chances not random... but IF I have to use random then I can work with it...

#28335 05/06/03 09:42 PM
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
The random number generated by $my_rand() in my last example will be between 1 and 10. Since it's random (for all intents and purposes), there's a 10% chance of that number equalling one, which is what the if condition checks for. If you have a message and you just want a 10% chance of something being added to that message you can use $iif() as an /if command in the middle of the line. For example:
Code:
on *:Text:-pickup:#:{
if (%choice == $nick) msg $chan $nick chooses to pick up the backpack and finds: a knife, 20 gold, [color:green]$iif($my_rand(1,33) == 1,[color:blue]the extra text here[/color])[/color]
}


I used $my_rand(1,33) there so it will choose a number between 1 and 33, making it roughly a 3% chance of returning 1.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
#28336 06/06/03 02:14 AM
Joined: Jun 2003
Posts: 12
W
Pikka bird
OP Offline
Pikka bird
W
Joined: Jun 2003
Posts: 12
so...
$iif($my_rand(1,33) == 1,the extra text here)

where it says , the extra text here) Is where I put the next set of code that I want to happen if in fact they do luck out?

#28337 06/06/03 11:22 AM
Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Yes, but only if the code consists of just $identifiers, text, and/or %variables to add to the end of that message. If you want other /commands to be performed then you'd have to use something like the example in my first post:
Code:
msg $chan something
if ($my_rand(1,33) == 1) {
some_other_command extra things
}


If you still don't get what I mean check the helpfile under $iif and If-then-else - that'll explain how they work better than I can.


Spelling mistakes, grammatical errors, and stupid comments are intentional.

Link Copied to Clipboard