I've created this code that generates a random integer value each day. So today the rand() will return 3 for example each time I call it, tomorrow it generates another value instead.
std::vector<MyObject> myList = getMyList();
time_t t = time(NULL);
tm *lt = localtime(&t);
int seed = lt->tm_mday + lt->tm_mon + 1 + lt->tm_year + 1900;
srand(seed);
dailyRand=rand();
int selectedID=rand() % myList.size();
MyObject obj = myList.at(selectedID);
Now i'm trying to generate a weekly rand instead, but that changes on Tuesday at 00:00 and stay the same for the entire week, until the next.
Unfortunately i didn't find any solution yet. Could someone help me?
UPDATE:
My Attempt (and maybe solution?):
seed = trunc((lt->tm_yday + (lt->tm_year * 365)+1) / 7);
srand(seed);
rand();
It should get the number of days since year 0 (+1 because 01/01/1990 was Monday and i need Tuesday instead), then the divided by 7 should give me the number of the week from 1900 starting from Tuesday. But i've tried with some values and
UPDATE 2: i've achieved it following @mksteve reply
time_t t = time(NULL);
tm *lt = localtime(&t);
int firstTuesday = 432000; // Tuesday 1970/01/06 at 00:00
int seed = (((t - firstTuesday)/60/60/24))/7;
srand(seed);
int r=rand();