简体   繁体   English

从文件中读取随机行? C#

[英]Read random line from a file? c#

I have a text file with few hundred lines, the structure is pretty simple. 我有一个几百行的文本文件,结构很简单。

firstname lastname 名字姓氏

I need to pick out a random firstname & listname from the file. 我需要从文件中挑选一个随机的firstname和listname。

string[] lines = File.ReadAllLines(...); //i hope that the file is not too big
Random rand = new Random();
return lines[rand.Next(lines.Length)];

Another (and maybe better) option is to have the first line of the file contain the number of records in it and then you don't have to read all the file. 另一个(也许更好)选项是让文件的第一行包含其中的记录数,然后您不必读取所有文件。

Read each line keeping a count, N, of the lines you have seen so far. 读取每行,保持您目前所看到的行数N。 Select each line with probability 1/N, ie, the first line will always be chosen, the second line will be chosen 1/2 times to replace the first, the third 1/3 times, ... This way each line has a 1/N probability of being the selected line, you only have to read the file once, and you don't need to store all of the file in memory at any given time. 选择每条线的概率为1 / N,即始终选择第一条线,第二条线将被选择1/2次以替换第一条线,第三条线为1/3次,...这样每条线都有一条线作为所选行的1 / N概率,您只需要读取一次文件,并且您不需要在任何给定时间将所有文件存储在内存中。

Here's an implementation that can be adapted for your needs. 这是一个可以根据您的需求进行调整的实现。

public string RandomLine( StreamReader reader )
{
    string chosen = null;
    int numberSeen = 0;
    var rng = new Random();
    while ((string line = reader.ReadLine()) != null)
    {
        if (rng.NextInt(++numberSeen) == 0)
        {
            chosen = line;
        }
    }
    return chosen;
}

Based on a C implementation for selecting a node from an arbitrarily long linked list. 基于用于从任意长链表中选择节点的C实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM