lundi 1 mai 2017

Select Random Items but with some items having a higher percentage of being selected?

I'm using MVC C# and SQL Sever.

I have about 100 items that I have been selecting 2 random items from. This was straight forward sorting by a new guid.

 vm = vm.OrderBy(c => Guid.NewGuid()).Take(2).ToList();

But now I want to be able to mark a subset of items to have a higher percentage (20%) of being selected and then at a later date another subset to be 50%.

The list of items is static. It will always be about 100 items.

I was just going to brute force it and add 20% more of the selected items to a list and them just do the random selection twice.

vm1 = vm.OrderBy(c => Guid.NewGuid()).Take(1).Single();
vm2 = vm.OrderBy(c => Guid.NewGuid()).Where(_ => _.Id != firstSelectedID).Take(1).Single();

But was thinking there might be a cleaner more performant way in SQL or something that I'm missing in C#




Aucun commentaire:

Enregistrer un commentaire