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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:19:24+00:00 2026-05-20T08:19:24+00:00

I’m trying to generate semi-random subsets with some restrictions. Here are the variable descriptions

  • 0

I’m trying to generate semi-random subsets with some restrictions.

Here are the variable descriptions with example values:

  • ObjCount – the number of objects (12)
  • VisibleCount (AKA SetSize) – the number of objects per set (6)
  • SetCount – the number of sets (12)
  • ObjAppearances – the number of set in which an object appears = SetCount * VisibleCount / ObjCount

I need to produce a given number of sets (SetCount) that follow these rules:

  1. Each set is a collection of objects, but no object can be in a single set more than once.
  2. Furthermore, each object should be in the same number of sets. If it doesn’t devide evenly, then the number sets in which an object appears can be off by 1 (some objects are in 4 sets, and others are in 5). I’ll try to avoid this situation, so it’s not critical.

It’s turning out to be far less trivial than I initially thought. Could anyone help me with some code/psuedocode? A solution to a generalized version would be very helpful too.

Thanks in advance.

Edit: VisibleCount is the set size. The number of times an object appears (ObjAppearances) is SetCount * VisibleCount / ObjCount

Edit2: I should also add that I want the sets to be fairly random. If the sets all have sequential objects (e.g. set1:5,6,7 set2:3,4,5 set3:10,11,0), the solution isn’t useful. Sorry for not making that clear.

Edit3: Here is a solution that DOES NOT work. (In C#)

static void Main(string[] args)
{
    var ObjectCount = 12;
    var SetSize = 6;
    var SetCount = 12;

    var Sets = Enumerable.Range(0, SetCount).Select(i => new List<int>()).ToArray(); // a SetCount-sized array of lists
    var ObjectAppearances = SetSize * SetCount / ObjectCount;
    var rand = new Random();

    // fill the sets
    for (int obj = 0; obj < ObjectCount; obj++)
    {
        for (int a = 0; a < ObjectAppearances; a++)
        {
            // get the collection of sets that are not full
            var nonFullSets = Sets.Where(s => s.Count < SetSize);
            // get the collection of non-full sets without obj
            var setsWithoutObj = nonFullSets.Where(s => !s.Contains(obj));
            ///////////////////////
            // Here is the problem. All of the non-full sets may already 
            // have a copy of obj
            ///////////////////////
            // choose a set at random
            var currentSetIndex = rand.Next(setsWithoutObj.Count());
            var currentSet = setsWithoutObj.ElementAt(currentSetIndex);
            // add the object
            currentSet.Add(obj);
        }
    }

    // randomize the order within each set and output each
    for (int i = 0; i < SetCount; i++)
    {
        var randomlyOrderedSet = Sets[i].OrderBy(obj => rand.Next());
        Sets[i] = new List<int>(randomlyOrderedSet);

        // output
        foreach (var obj in Sets[i])
            Console.Write(string.Format("{0}, ", obj));
        Console.WriteLine();
    }
    Console.ReadLine();
}

Here’s the Solution – An implementation of MizardX’s answer

static void Main(string[] args)
{
    var ObjectCount = 12;
    var SetSize = 6;
    var SetCount = 10;
    var rand = new Random();

    // make a matrix [SetCount][ObjectCount]
    var Matrix = new int[SetCount][];
    for (int s = 0; s < SetCount; s++)
        Matrix[s] = Enumerable.Repeat(0, ObjectCount).ToArray();

    // put approximately the same number of objects in each set by
    // adding sequential objects to sequential sets (not random)
    for (int s = 0; s < SetCount; s++)
    {
        var firstObject = (int)Math.Ceiling((double)s * ObjectCount / SetCount);
        for (int i = 0; i < SetSize; i++)
        {
            var o = (firstObject + i) % ObjectCount;
            Matrix[s][o] = 1;
        }
    }

    // output the result
    for (int s = 0; s < SetCount; s++)
    {
        for (int o = 0; o < ObjectCount; o++)
        {
            Console.Write(string.Format("{0}, ", Matrix[s][o]));
        }
        Console.WriteLine();
    }
    Console.WriteLine();

    // shuffle sets
    Matrix = Matrix.OrderBy(s => rand.Next()).ToArray();
    // make a new matrix for shuffle objects
    var objOrder = Enumerable.Range(0, ObjectCount).OrderBy(o => rand.Next()).ToArray();
    var MatrixSuffled = new int[SetCount][];
    for (int s = 0; s < SetCount; s++)
        MatrixSuffled[s] = Enumerable.Repeat(0, ObjectCount).ToArray();
    for (int o = 0; o < ObjectCount; o++)
    {
        var oldObj = o;
        var newObj = objOrder[o];
        for (int s = 0; s < SetCount; s++)
        {
            MatrixSuffled[s][newObj] = Matrix[s][oldObj];
        }
    }

    // check and output the result
    var objectCounters = Enumerable.Repeat(0, ObjectCount).ToArray();
    for (int s = 0; s < SetCount; s++)
    {
        var objectsInThisSet = 0;
        for (int o = 0; o < ObjectCount; o++)
        {
            objectsInThisSet += MatrixSuffled[s][o];
            objectCounters[o] += MatrixSuffled[s][o];
            Console.Write(string.Format("{0}, ", MatrixSuffled[s][o]));
        }
        Console.Write(string.Format("  {0}", objectsInThisSet));
        Console.WriteLine();
    }
    // output object count
    Console.WriteLine();
    for (int o = 0; o < ObjectCount; o++)
        Console.Write(string.Format("{0}  ", objectCounters[o]));
    Console.ReadLine();
}
  • 1 1 Answer
  • 2 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-20T08:19:25+00:00Added an answer on May 20, 2026 at 8:19 am
    1. Create a ObjCount × SetCount matrix, and fill it with ones and zeros so that each column contains VisibleCount ones, and each row contains an (almost) equal number of ones. Order is irrelevant at this point.
    2. Shuffle the columns (and the rows, if ObjCount does not divide SetCount × VisibleCount evenly).
    3. For each column i, if the cell at row j equals 1, add object j to set i.

    For 12 objects, 6 sets and 3 visible, the initial matrix might look like this:

    1 0 0 0 0 0
    1 0 0 0 0 0
    1 1 0 0 0 0
    0 1 0 0 0 0
    0 1 1 0 0 0
    0 0 1 0 0 0
    0 0 1 1 0 0
    0 0 0 1 0 0
    0 0 0 1 1 0
    0 0 0 0 1 1
    0 0 0 0 1 1
    0 0 0 0 0 1
    

    And after shuffle:

    1 0 1 0 0 0
    0 0 0 0 1 0
    1 1 0 0 0 0
    0 0 0 1 1 0
    0 1 0 0 0 0
    0 0 0 1 0 0
    0 0 1 0 0 0
    1 0 1 0 0 0
    0 0 0 1 0 0
    0 0 0 0 1 1
    0 1 0 0 0 1
    0 0 0 0 0 1
    

    Resulting in sets:

    {1,3,8}
    {3,5,11}
    {1,7,8}
    {4,6,9}
    {2,4,10}
    {10,11,12}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.