mIRC Homepage
Posted By: FlowCtrl Read Random Line - What am I doing wrong? - 23/04/15 04:04 AM
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!
Posted By: sparta Re: Read Random Line - What am I doing wrong? - 23/04/15 05:39 AM
You looking for $rand, look it up in the help file. /help $rand
Posted By: Wims Re: Read Random Line - What am I doing wrong? - 23/04/15 03:11 PM
$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
$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)
Posted By: Wims Re: Read Random Line - What am I doing wrong? - 23/04/15 05:32 PM
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.
Thank you everyone for your responses! The information provided will be very helpful.
© mIRC Discussion Forums