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 6715387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:36:31+00:00 2026-05-26T08:36:31+00:00

Get ‘n’ random string from a Arraylist of string. Where n= number of string

  • 0

Get ‘n’ random string from a Arraylist of string. Where n= number of string I want from the list it can be 1,2,3….? Suppose that I have Array list of N Strings if I want to choose Random four strings without repetition. How can I do it? The code should be in C#. Though I have done this, but What is the best way?

  • 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-26T08:36:32+00:00Added an answer on May 26, 2026 at 8:36 am

    Try this:

    class Program
    {
        static void Main(string[] args)
        {
            int myRandomIndex = 0;
            var myList = new List<string>(new[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" });
            var results = new List<string>();
            var r = new Random(DateTime.Now.Millisecond);
            for (int ii = 0; ii < 4; ii++)
            {
                myRandomIndex = r.Next(myList.Count);
                results.Add(myList[myRandomIndex]);
                myList.RemoveAt(myRandomIndex);
            }
    
            Console.WriteLine(string.Join("", results));
            Console.ReadKey();
        }
    }
    

    To keep each chosen string unique (prevent duplicates) I remove it from the source list as it is used. You should also do a myList = myList.Distinct() before using the list, to make sure you don’t have duplicates in it to begin with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Cant get this to work can any one help. List the part number, part
I get an XML file From a web service. Now I want to get
.get() converts a jQuery object to a DOM element that Javascript can use without
i get date from xml in this format: 7/16/2010 (mm/dd/yyyy) i have in my
GET_COOKIE('CURRENT_IM') will return a number saved in the CURRENT_IM cookie. How can I get
get / and inside controller I have action that depends on browser When I
get an array of bluetooth ID's Broadcast the bluetooth signal manually
We get a large amount of data from our clients in pdf files in
I get a URL from a user. I need to know: a) is the
I get this error: Can't locate Foo.pm in @INC Is there an easier way

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.