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

  • Home
  • SEARCH
  • 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 541331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:19:57+00:00 2026-05-13T10:19:57+00:00

I want to get random number of lines(let’s say 2 ) group by Code

  • 0

I want to get random number of lines(let’s say 2 ) group by Code from text file.

for e.g.

4581511:         50.27:        AT
1223522:         86.99:        AT
7456117:         68.59:        QW
5261789:         39.17:        QW
…..
…..

Text File

bookNumber      Price     Code
4581511:         50.27:        AT
7841522:         26.13:        AT
7353532:         96.13:        AT
1223522:         86.99:        AT
8415621:         89.70:        IT
8411442:         82.42:        IT
4555577:         19.14:        IT
7655577:         65.45:        IT
2754831:         35.44:        DR
1364449:         82.47:        DR
4545454:         45.65:        DR
8795457:         78.36:        DR
5261789:         39.17:        QW
7845522:         10.42:        QW
7456117:         68.59:        QW
4346129:         23.78:        QW

I got this far and I am geting 2 lines which are not random but in sequence

Code

static IEnumerable<string> ReadLines(string path)
    {

        using (var file = File.OpenText(path))
        {
            string line;
            while ((line = file.ReadLine()) != null)
            {
                if (line.Contains(":"))
                {
                    yield return line;
                }
            }
        }
    } 

    public static IEnumerable<string> GetrandomLines()
    {

        string filepath = "file location";
        var readTextFile = ReadLines(filepath);
        var codeGroup = readTextFile.GroupBy(line => line.Substring(line.Length - 2))
                          .Select(g => new
                          {
                            value = g.Key,
                            count = g.Count()
                           });


        foreach (var item in codeGroup)
        {
            Random randomLineGenerator = new Random(DateTime.Now.Millisecond);
             var randomLines = (from x in readTextFile
                                where x.Substring(x.Length - 2) == item.value
                                select x).Skip(randomLineGenerator.Next(0, item.count)).Take(2);
             foreach (var line in randomLines)
             {
                 yield return line;
             }
        }

    }

Any Ideas?

Thanks

  • 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-05-13T10:19:58+00:00Added an answer on May 13, 2026 at 10:19 am

    Guid version, the random values can’t be the same.

    public static IEnumerable<string> GetrandomLines2(string filePath, int lines)
    {
        return ReadLines(filePath)
            .GroupBy(line => line.Substring(line.Length - 2))
            .SelectMany(s => s.OrderBy(g => Guid.NewGuid()).Take(lines));
    }
    

    Random version, the two random values may be the same.

    public static IEnumerable<string> GetTwoRandomLines(string filePath)
    {
        var codeGroup = ReadLines(filePath)
            .GroupBy(line => line.Substring(line.Length - 2));
    
        Random rnd = new Random(DateTime.Now.Millisecond);
    
        foreach (var item in codeGroup)
        {
            yield return item.Skip(rnd.Next(item.Count())).FirstOrDefault();
            yield return item.Skip(rnd.Next(item.Count())).FirstOrDefault();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get a random number in pascal from between a range. Basically
i am trying to get a random number of items from an an array
I want to get 20 random numbers between 1 to 100, but the number
I want to get the size of an http:/.../file before I download it. The
I have the following function: //Function to get random number public static int RandomNumber(int
I am trying to find a way to get a random selection from a
I want to get started doing some game development using Microsoft's XNA. Part of
I want to get the MD5 Hash of a string value in SQL Server
I want to get my databases under version control. I'll always want to have
I want to get an overview of files that are updated in TFS (that

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.