mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Apr 2015
Posts: 2
F
Bowl of petunias
OP Offline
Bowl of petunias
F
Joined: Apr 2015
Posts: 2
Greeting Everyone,

I'm just starting to learn how to script in mIRC.

I ran across an issue where if I had more than 1 $read(file.txt,n) in a command, it would return the same random value. A simple example below:

on *:text:!select:# {
msg $chan $read(list.txt,n)
msg $chan $read(list.txt,n)
}

What I really want to do is set the variables to 2 seperate random values from list.txt. For example:

on *:text:!select:# {
set %var1 $read(list.txt,n)
set %var2 $read(list.txt,n)
}

Does anyone have a suggestion? I'm reading up on the wiki, but can't seem to come to a conclusive solution yet.

Any help is greatly appreciated! Thanks!

Last edited by FlowCtrl; 23/04/15 04:25 AM.
Joined: Feb 2003
Posts: 3,432
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Feb 2003
Posts: 3,432
You looking for $rand, look it up in the help file. /help $rand


if ($me != tired) { return } | else { echo -a Get a pot of coffee now $+($me,.) }
Joined: Jul 2006
Posts: 4,150
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,150
$rand isn't going to be really helpful because $read already reads a random line.
You simply need to stock the first value and to find a new random one as long as the second one you get is the same as the first value

var %var1 $read(list.txt,tn)
while ($read(list.txt,tn) == %var1) /
var %var2 $v1


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
$rand() would be helpful because you can make sure you have two dissimilar numbers before pulling from the file. Yes, $read() can perform its own random selection, but why scratch the harddrive a dozen times before two unique lines are returned?

var %file = list.txt
var %lines = $lines(%file)
if (%lines < 2) { return }
var %r1 = $rand(1,%lines)
var %r2 = $rand(1,%lines)
while (%r2 == %r1) { var %r2 = $rand(1,%lines) }
var %line1 = $read(%file,n,%r1)
var %line2 = $read(%file,n,%r2)

Last edited by Raccoon; 23/04/15 06:04 PM. Reason: Corrected $rand(2,%lines) was incorrect. Thanks Ouims.

Well. At least I won lunch.
Good philosophy, see good in bad, I like!
Joined: Jul 2006
Posts: 4,150
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,150
Right, well I didn't even consider this as an issue, I was just saying you wouldn't need $rand to get randomness but indeed, avoiding access to the disk is much better.
Btw:
Code:
var %r2 = $rand(2,%lines)
Why do you start at 2 here? Most of the time the first two random line/number will be different, meaning the second line being read by %r2 can never be the first, I think it should start at 1 and allow the loop to handle the equality, or it's not as uniform as one would want imo.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Apr 2015
Posts: 2
F
Bowl of petunias
OP Offline
Bowl of petunias
F
Joined: Apr 2015
Posts: 2
Thank you everyone for your responses! The information provided will be very helpful.


Link Copied to Clipboard