-5

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();
Joseph
  • 1,029
  • 13
  • 26
  • 1
    "_i didn't find any solution yet_" Did you, at least, tried something? Please show your attempts with explanations of why it doesn't suit your needs. – Algirdas Preidžius Oct 18 '17 at 18:00
  • 1
    What problem did you have when you extended your daily solution to be weekly? – Fantastic Mr Fox Oct 18 '17 at 18:01
  • 2
    I wonder why so many downvotes – Francesco Borzi Oct 18 '17 at 18:02
  • how can i extend solution to weekly if we don't have any information about the week of the month on tm? maybe i'm missing something – Joseph Oct 18 '17 at 18:05
  • @Joseph Does [this](https://stackoverflow.com/questions/274861/how-do-i-calculate-the-week-number-given-a-date) help? – user0042 Oct 18 '17 at 18:07
  • @Joseph You have information about _days since January 1_ (`tm_yday`), and _days since Sunday_ (`tm_wday`). One could use such information to detect if week has changed. – Algirdas Preidžius Oct 18 '17 at 18:08
  • i've posted a possible solution but i don't think it works – Joseph Oct 18 '17 at 18:32
  • 1
    You are heading off into strange directions, my friend. You claim to want want a new random number generated every . What you are doing is changing the seed and getting the first random number for that new seed. End result is your random number is not random at all. It is easily calculable given the day (or week). Can you please clarify your goal? – user4581301 Oct 18 '17 at 19:02
  • sorry i forgot to specify that i use this generated random on a list of X elements to select 1 randomly each day doing: int selectedID = rand() % list.size(); It's not important that it's predictable but i have to retrieve an element in a casual order each day (in my first case). The second case instead that is the question itself, is: how can i do the same where the rand remains the same for 7 days and change each Tuesday? – Joseph Oct 18 '17 at 23:49

1 Answers1

1

You are looking for a value which varies at a particular time.

The easiest way to get this to work is to have a 0 time which suits you.

So calculate a Tuesday in January 1970, at exactly the right time.

Then remove that from the current time_t. That will give you the number of seconds since that time.

All you need to do, is divide by the 24 * 60 * 60 * 7 to get the repeating value for each Tuesday.

mksteve
  • 12,614
  • 3
  • 28
  • 50
  • My code now perfectly works, following your suggestion. Thank you! I'll update my first post – Joseph Oct 19 '17 at 00:07