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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:16:06+00:00 2026-05-18T08:16:06+00:00

So i have the following generic list: var topTenSomething = new List<Something>(); Here is

  • 0

So i have the following generic list:

var topTenSomething = new List<Something>();

Here is Something:

public class Something
{
   public string Name { get; set; }
   public int Rank { get; set; }
}

So i want to randomly assign the “Rank” property, but it needs to be ordered from 1-number of items in the collection.

So if the collection has 3 items, i want to randomly assign ranks from 1 to 3:

  1. Some Name
  2. Some Other Name
  3. Something Else

Then next time, it could be:

  1. Some Other Name
  2. Some Name
  3. Something Else

Know what i mean?

Not sure how to do it – any ideas?

This is for a simple R&D prototype – so don’t worry about performance/why i am doing this. (the real one will have rank assigned by database)

Happy with either a LINQ/non-LINQ version – as long as it works.

  • 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-18T08:16:07+00:00Added an answer on May 18, 2026 at 8:16 am

    Like this:

    var rand = new Random();
    var sequence = Enumerable.Range(0, list.Count).OrderBy(i => rand.Next()).ToList();
    
    for(var i = 0; i < list.Count; i++)
        list[i].Rank = sequence[i];
    

    If you want the list to be sorted by the random rank:

    var rand = new Random();
    list.Sort((a, b) => rand.Next(-1, 2));    //Exclusive upper bound
    for(var i = 0; i < list.Count; i++)
        list[i].Rank = i;
    

    However, this is not a valid ordering (a < b does not imply b > a) and may cause unexpected results.

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

Sidebar

Related Questions

I have a Product class: class Product { public string Name { get; set;
I have the following code files: public interface IMod { string Name { get;
I have the following piece of code: public List<myTask> MyTask { get; set; }
So I have created the following script: class spImageUpload() { private static System.Collections.Generic.List<string> keywords;
Lets say I have a generic list of the the following objects: public class
I have the following generic class: public class MessageProcesser<T> where T : Message Inside
I have the following generic repository: public class GenericRepository<TEntity> where TEntity : class {
I have the following code implementation of my generic singleton provider: public sealed class
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
Let's say I have the following class. MyClass<T> { public void MyMethod(T a, List<T>

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.