mIRC Home    About    Download    Register    News    Help

Print Thread
to random #169661 26/01/07 04:47 PM
Joined: Oct 2006
Posts: 342
L
learn3r Offline OP
Fjord artisan
OP Offline
Fjord artisan
L
Joined: Oct 2006
Posts: 342
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


learn learn learn
Re: to random [Re: learn3r] #169667 26/01/07 07:18 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156

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)
}

Re: to random [Re: DJ_Sol] #169688 26/01/07 10:28 PM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
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.


Invision Support
#Invision on irc.irchighway.net
Re: to random [Re: Riamus2] #169704 27/01/07 12:03 AM
Joined: Oct 2006
Posts: 342
L
learn3r Offline OP
Fjord artisan
OP Offline
Fjord artisan
L
Joined: Oct 2006
Posts: 342
both working perfectly thanks to you guys


learn learn learn
Re: to random [Re: Riamus2] #169722 27/01/07 04:02 AM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline
Hoopy frood
Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
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

Re: to random [Re: DJ_Sol] #169725 27/01/07 05:37 AM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
No problem. It's a good way to randomly play music as well.


Invision Support
#Invision on irc.irchighway.net