Get random line (optimized)

by Ricardo Fernández Serrata

Version 1 (July 20, 2021)

Download (12 downloads)

Shows how to get a random line of text from a file, without shuffle()

Tips:
Only use split() when the flow reads or re-reads a file.
NEVER use split() repeatedly on the same text, save its output to a variable to avoid extra processing.

Don't read a file repeatedly if the file hasn't been modified, only read when the file gets updated.

Remember that split() assumes \n is a delimiter, not a terminator. If \n is used to end every line (including EOF), the returned array will have an empty string at the end

4.0 average rating from 1 reviews

5 stars
0
4 stars
1
3 stars
0
2 stars
0
1 star
0
Reports
0

Rate and review within the app in the Community section.