I want to randomly pick a set of 3 from a collection (1-10) based on a seed that is a date. I want to pick 3 new items every day not repeating the ones i've picked the day before. Picking the items is not the problem but how do i know which ones have been picked yesterday, can i reconstruct that with only knowing the date?
-
1I'm voting to close this question as off-topic because this is not a free coding service. – Ondrej Tucny Jun 09 '17 at 10:02
-
There are a lot of websites which work this way - you give some requirements, and someone implements those requirements for you (probably for some money). But StackOverflow works in different way - you try to implement requirements on your own, and if you have some problems (error or unexpected results), then you give problem description, your current code and ask people to help you. – Sergey Berezovskiy Jun 09 '17 at 10:04
-
I've edited my question. I don't want a solution, i want to know if this is even possible with the given constraints. – Arnold from Tinytouchtales Jun 09 '17 at 10:14
2 Answers
Yes, you can. Class Random
provides a pseudo random sequences. When you initiate it with same seed value, sequence always will be the same (check Random(int seed) constructor). So
how do i know which ones have been picked yesterday, can i reconstruct that with only knowing the date?
To get numbers which you picked yesterday, you should use same seed as you have used yesterday. E.g. you can use the value of Ticks
from DateTime
object for given date.
var today = DateTime.Today();
var random = new Random(today.Ticks);
var todayItems = items.OrderBy(x => random.Next()).Take(3).ToList();
var yesterday = today.AddDays(-1);
random = new Random(yesterday.Tikcs);
var yesterdayItems = items.OrderBy(x => random.Next()).Take(3).ToList();
Now you compare items from today and from yesterday.

- 232,247
- 41
- 429
- 459
-
Hi, thanks for answering. This is what i do right now. But what i was asking for is this intermediate step of avoiding the same items in two consecutive days. Meaning if i get 1,2,3 on day one i want to exclude 1,2,3 from being chosen on day two. I could avoid that by just saving the pick of day one somewhere. But let's assume i have no way of doing that, would it still be possible to do this? – Arnold from Tinytouchtales Jun 10 '17 at 09:38
If you have 10 items in your collection, then you can represent your subset of 3 items as a binary number with three, and only three bits set. Just set the bits corresponding with the three selections made. If you pick items 0, 5 and 7 in the collection, then set bits 0, 5 and 7 in a 10 bit number. Store that number, and use it to prevent selecting the same set of three again the next day. Just compare yesterday's number with today's number. A 10 bit number will easily fit into a C# integer.

- 15,344
- 1
- 24
- 38
-
Thanks, this actually was my original question, is it possible to do it without storing the information of which 3 items where picked on each day to compare them on the next day. – Arnold from Tinytouchtales Jun 10 '17 at 09:39
-
You have to store the information somehow, otherwise you cannot tell if you are repeating the same selection. – rossum Jun 10 '17 at 11:17