Can someone give me the code to choose a random line from a specified file?
Thank you
Printable View
Can someone give me the code to choose a random line from a specified file?
Thank you
NOT TESTED:!
HTH! :)Code:Dim a as String, I as Integer, K as Integer, N as Integer
Open File For Input as #1
While Not EOF (1)
Line Input #1, a ' use Line Input method to avoid commas, etc.
I = I + 1 ' counts the available lines
Next
Close
Randomize
K = Rnd * I ' selects a number in the order of the available lines
Open File For Input as #1
For N = 1 to K
Line Input #1, a ' displaces pointer to randomly selected line
Next
Line Input #1, a ' inputs that line
Close
' Now Do Your Stuff With Your Line!