У вас две проблемы.
Вы не знаете, сколько строк вы получили. Вы можете использовать считыватель строк для l oop по строкам, чтобы получить количество строк.
Вам необходимо сгенерировать случайное число от 0 до количества строк. Для этого вы можете использовать класс Random.
Собрав их вместе, вы можете использовать что-то вроде следующего кода:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
WebClient web = new WebClient();
string text = web.DownloadString("https://pastebin.com/raw/g064BJhZ"); //this is your call, but it only returns 2 lines. Not very interesting.
StringBuilder sb = new StringBuilder(); //we make a test string with more lines:
for (int i = 0; i < 25; i ++)
{
sb.AppendLine($"This is line {i}");
}
List<string> lines = new List<string>(); //we want to hold all the lines
//you could be more efficient here, just showing you an easy to understand
using (StringReader sr = new StringReader(sb.ToString()))
{
while (sr.Peek() >= 0) //while we have more lines
{
lines.Add(sr.ReadLine()); //add this line to our list
}
}
for(int i = 0; i < 5; i++) //now lets output some random lines:
{
//this says using a random number, between 0 and the number of lines,
// write the line to the console.
Console.WriteLine(lines[RandomNumber(0, lines.Count()-1)]);
}
}
public static int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
}
}
Мой вывод: это строка 4 Это строка 12 Это строка 12 Это строка 19 Это строка 21