mIRC Homepage
Posted By: learn3r to random - 26/01/07 04:47 PM
Code:
    set %trivia.rand $rand(1,$lines(trivia.txt))
    set %trivia.n $read(trivia.txt,%trivia.rand)
    set %trivia.q $gettok($read(trivia.txt,%trivia.rand),1,42)
    set %trivia.a $gettok($read(trivia.txt,%trivia.rand),2,42)


that reads trivia.txt

how to random read two texts?

like :
trivia1.txt and trivia2.txt
Posted By: DJ_Sol Re: to random - 26/01/07 07:18 PM

Code:
var %r = $rand(1,2), %t
%t = $iif(%r == 1,trivia1.txt,trivia2.txt)
set %trivia.rand $rand(1,$lines(%t))
    set %trivia.n $read(%t,%trivia.rand)
    set %trivia.q $gettok($read(%t,%trivia.rand),1,42)
    set %trivia.a $gettok($read(%t,%trivia.rand),2,42)


This is with just two texts. If you have more than two you want to randomly choose, you can say ...

var %r = $rand(1,2), %t
if (%r == 1) %t = text1.txt
elseif (%r == 2) %t = text2.txt
elseif (%r == 3) %t = text3.txt
etc.

Or if there is a bunch you can even make a custom identifier to do all this for you.

alias ran_triv {
var %r = $rand(1,5)
if (%r == 1) return text1.txt
elseif (%r == 2) return text2.txt
elseif (%r == 3) return text3.txt
elseif (%r == 4) return text4.txt
elseif (%r == 5) return text5.txt
}

then in your code you would say ...

var %t = $ran_triv
set %trivia.rand $rand(1,$lines(%t))
set %trivia.n $read(%t,%trivia.rand)
set %trivia.q $gettok($read(%t,%trivia.rand),1,42)
set %trivia.a $gettok($read(%t,%trivia.rand),2,42)


or this custom identifier if the files are all named trivia(number).txt. Like trivia1.txt trivia2.txt etc.
alias ran_triv {
var %r = $rand(1,5)
return $+(trivia,%r,.txt)
}
Posted By: Riamus2 Re: to random - 26/01/07 10:28 PM
I'd suggest using $findfile as it will keep the script short even with hundreds of text files. Just put all of your trivia files into one folder with no other .txt files in that folder.

Then, put this code somewhere... alias, on TEXT, etc.
Code:
var %trivia = $read($findfile("path\",*.txt,$rand(1,$findfile("path\",*.txt,0))))



Replace path\ with the path to your trivia files... for example, if your trivia files are in a folder called "trivia" and that is in your mIRC folder, you'd use:

Code:
var %trivia = $read($findfile("trivia\",*.txt,$rand(1,$findfile("trivia\",*.txt,0))))




That will read a random line from a random text file inside your trivia folder and set that line as the %trivia variable. It will work regardless how many files you have.
Posted By: learn3r Re: to random - 27/01/07 12:03 AM
both working perfectly thanks to you guys
Posted By: DJ_Sol Re: to random - 27/01/07 04:02 AM
Thanks Riamus, I was trying to figure out how to randomly pick a wav file from the sounds folder. This will work for me. smile
Posted By: Riamus2 Re: to random - 27/01/07 05:37 AM
No problem. It's a good way to randomly play music as well.
© mIRC Discussion Forums