Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8863267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:56:04+00:00 2026-06-14T15:56:04+00:00

How could one select a random line of text from a text file and

  • 0

How could one select a random line of text from a text file and set it in a variable to use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:56:05+00:00Added an answer on June 14, 2026 at 3:56 pm

    Here’s yet another approach. It reads the file name from the command line and uses a FOR /L loop to get to the calculated line number:

    @ECHO OFF
    FOR /F "" %%I IN ('FIND /C /V "" ^<%1') DO SET /A lines=%%I
    SET /A skip=%RANDOM%%%lines
    <%1 (
      FOR /L %%I IN (1,1,%skip%) DO (
        SET /P line=
      )
      SET line=
      SET /P line=
    )
    ECHO(%line%
    

    The FOR /F loop simply gets the number of lines in the file (the method is borrowed from @Aacini’s answer).

    A rather simplistic formula then calculates the number of lines to skip in the file.

    Next, the file is read. The FOR /L loop merely consumes the specified number of lines using a SET /P instruction. Following the loop, one more SET /P command reads the line that is eventually ECHOed.

    The above implementation is just to show the basic idea. It is not without issues, but some of them could easily be resolved:

    1. There’s no testing whether the parameter is indeed supplied. If it is absent, the script will break. You could add the necessary check at the beginning of the script like this:

      IF "%~1"=="" GOTO :EOF
      

      If there’s no parameter, this command terminates the script by sending the control to the end of the script (GOTO :EOF).

    2. The file specified might not exist. Again, you could test that at the beginning, just after verifying that the parameter is supplied, to terminate the script if necessary:

      IF NOT EXIST %1 GOTO :EOF
      
    3. If the file is empty, the lines will be 0 and the subsequent expression using it will run into a division by zero error. Therefore, you’ll also need to test the resulting line count (and prevent the script from running further if the count is indeed 0). You can do that by adding the following line just after the FOR /F loop:

      IF %lines%==0 GOTO :EOF
      
    4. Like I said, the formula is somewhat simplistic. It doesn’t produce a number greater than 32767, which is the limitation of %RANDOM%. That might well be enough for you, but in case it is not, you could extend the range to 230-1 using two %RANDOM% calls like this:

      SET /A skip=(%RANDOM%*32768+%RANDOM%)%%lines
      

    So, here’s the same script again, amended to address the above mentioned issues:

    @ECHO OFF
    IF "%~1"=="" GOTO :EOF
    IF NOT EXIST %1 GOTO :EOF
    FOR /F "" %%I IN ('FIND /C /V "" ^<%1') DO SET /A lines=%%I
    IF %lines%==0 GOTO :EOF
    SET /A skip=(%RANDOM%*32768+%RANDOM%)%%lines
    <%1 (
      FOR /L %%I IN (1,1,%skip%) DO (
        SET /P line=
      )
      SET line=
      SET /P line=
    )
    ECHO(%line%
    

    One other note is that, if you like, you can add messages explaining the reason for the premature termination of the script. Basically, wherever you want to add the message, you’ll just need to replace the single

    GOTO :EOF
    

    with

    (ECHO your message & GOTO :EOF)

    For instance:

    IF NOT EXIST %1 (ECHO Error! File not found & GOTO :EOF)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How could one test whether a set of modules is installed, given the names
I would like to select a random record from a table but with a
I am trying to make Java select 1 random string from a given list.
Could one help translating following sed command so it does the same on aix
Problem in short: How could one implement static if functionality, proposed in c++11, in
When adding a new Entity Framework object to a database, how could one have
could someone one the current list of managed Beans or classes in Java EE
Could any one tell me how to enable SOCKET support in PHP ?
Could some one guide me to create a APS logon token in .Net programmatically?
Could any one please tell me the meaning of ++ with array in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.