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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:22:53+00:00 2026-05-11T10:22:53+00:00

I have a page that displays two objects and then the user picks one

  • 0

I have a page that displays two objects and then the user picks one of these. I record the preference and the combination in a MSSQL database and end up storing data like this:

UserId=1, BetterObjectId=1, WorseObjectId=2 

Now I would like to avoid showing that combination of objects (1,2 / 2,1) ever again.

So how do I generate random combinations to show the user excluding previously viewed combinations?

This seems like it should be a really straightforward question but like most programmers I’m short on sleep and coffee so your help is much appreciated 🙂

The very naive approach is something like this (and all calls to this function would have to be wrapped in a check to see if the user has already rated as many times as nCr where n is the item count and r is 2):

public List<Item> GetTwoRandomItems(int userId) {     Item i = null, i2 = null;     List<Item> r = null;      while (i == null || i2 == null)     {         r = GetTwoRandomItemsRaw();         i = r[0];         i2 = r[1];         if (GetRating(i.Id, i2.Id, userId) != null) /* Checks if viewed */         {             i = null;             i2 = null;         }     }     return r; }  private List<Item> GetTwoRandomItemsRaw() {     return Items.ToList().OrderBy(i => Guid.NewGuid()).Take(2).ToList(); } 

Edits

Using some SQL I can generate a list of all items that aren’t complete (i.e. there is a combination involving the item that the user hasn’t seen) but I don’t think is particularly useful.

I can also imagine generating every possible combination and eliminating already viewed ones before picking 2 random items but this is a another terrible solution.

A possibility (memory intensive for large n) is to generate all possible combinations and store the combinationId in the rating. Then I can just do a SELECT of all combinations WHERE combinationId IS NOT IN (SELECT combinationId FROM ratings WHERE userId=x) with some changes to reflect the symmetric relationship of combinations.

  • 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. 2026-05-11T10:22:53+00:00Added an answer on May 11, 2026 at 10:22 am
    Table Item: ItemId Table Rating: UserId, ItemId1, ItemId2, WinnerId 

    If you require that ItemId1 < ItemId2 in the Rating table, you only have to check the Rating table once.

    var pair = db.Items.Join(db.Items,   i1 => i1.ItemId,   i2 => i2.ItemId,   (i1, i2) => new {i1, i2} )  //produce all pairs .Where(x => x.i1.ItemId < x.i2.ItemId) //filter diagonal to unique pairs .Where(x =>    !db.Ratings   .Where(r => r.UserId == userId     && r.ItemId1 == x.i1.ItemId     && r.ItemId2 == x.i2.ItemId)   .Any() //not any ratings for this user and pair ) .OrderBy(x => db.GetNewId()) //in-database random ordering .First();  // just give me the first one  return new List<Item>() {pair.i1, pair.i2 }; 

    Here’s a blog about getting ‘random’ translated into the database.

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

Sidebar

Related Questions

I have a test page that displays two images. One called hello.bmp and another
I have a page that displays a table in two different modes. In each
I have a page that displays a list of tickets. On each of these
I have a very straightforward HTML page that displays content in two columns. To
I have a one page website that has two seperate forms that need to
I have a page that displays a list with a of elements with a
i have a page that displays large datasets into html tables. how can i
I have a page that displays search results and has a DOM like the
I have a page that displays messages and I want it to work just
I have a page that displays posts and I want to get the name

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.