Reading a random line from a txt file.

Hello! I think this is probably simple but I am not quite good at this yet.

I want to read a random line from a text file of 100 million lines.

I was thinking I could set up a loop that uses rand() to just select the random line... So I would have (rand()%x)+1 where x == 100million and where after each loop x -= 1 .

so what I need to do is read a random line and then delete that random line from the file so that I don't read the same line twice and don't read a blank line. Any commands I should look up? I read something about fseek and I am still looking so I may find the answer before someone replies..

but thank you to anyone that helps.
Adaptation of an old algorithm (Brian Kernighan?):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <random>
#include <ctime>
#include <algorithm>

// invariant: file contains at least n non-empty lines
std::vector<std::string> pick_n_random_lines( const std::string& path, std::size_t n )
{
    static std::mt19937 rng( std::time(0) ) ;
    static const auto random = []( std::size_t min, std::size_t max )
    { return std::uniform_int_distribution<std::size_t>(min,max)(rng) ; } ;

    std::vector<std::string> selected ;

    std::ifstream file(path) ;
    std::string line ;
    std::size_t nlines = 0 ;
    while( std::getline( file, line ) )
    {
        if( !line.empty() )
        {
            if( selected.size() < n ) selected.push_back(line) ; // select the first n
            else // replace a random selected line with probability n / #lines
                if( random(0,nlines) < n ) selected[ random(0,n-1) ] = line ;

            ++nlines ;
        }
    }

    std::shuffle( selected.begin(), selected.end(), rng ) ;
    return selected ;
}

Topic archived. No new replies allowed.