0

I want to compute some number for each given day. The actual number does not matter, it should just be somewhat different but it must be stable.

So I have done the following so far:

now = new Date();

begin_of_week = new Date(now.getFullYear(), now.getMonth(), now.getDate() - now.getDay() + 1);

for (idx in [0, 1, 2, 3, 4]) {
    seed = parseInt(idx)
        + begin_of_week.getFullYear() * 400,
        + begin_of_week.getMonth() * 31,
        + begin_of_week.getDate();

    // ...
}

I expect that this gives me some seed for each day of the week from Monday to Friday. However, I different numbers for the days, but I now have the same numbers as last week.

JavaScript's type system is a mystery to me. I would expect that all those are int and they just get added up. It makes a difference whether I put the idx at the beginning or the end of the chain. And the parseInt, which should do nothing, actually changes the results.

What is going on here? How can I just add those numbers?

Martin Ueding
  • 8,245
  • 6
  • 46
  • 92
  • You should not need `parseInt` here. You should [not use `for…in` enumerations on arrays!](https://stackoverflow.com/q/500504/1048572) – Bergi Nov 14 '17 at 11:29
  • Ouch, the `for ... in` makes the loop variable a string. This explains it, of course! – Martin Ueding Nov 14 '17 at 11:44
  • 1
    Also an issue are the stray commas after `400` and `31`. I don't know how they got there, but this of course also is a problem. – Martin Ueding Nov 14 '17 at 11:50

1 Answers1

0

I expect that this gives me some seed for each day of the week from Monday to Friday. However, I different numbers for the days, but I now have the same numbers as last week.

This is because your begin_of_week is going to be same for entire week as you have given

now.getDate() - now.getDay() + 1

which essentially ensures that your date parameter is always offset'ed by number of days passed already in the week.

I would expect that all those are int and they just get added up.

You are assigning the value to seed rather than incrementing the seed value by the other values, so this is as good as

seed = parseInt(4)
        + begin_of_week.getFullYear() * 400,
        + begin_of_week.getMonth() * 31,
        + begin_of_week.getDate();

If you want to keep incrementing seed value, then use += instead of =

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • I explicitly add the `idx` to the `seed` in order to get something different for each day in the week. I don't see the need for `+=`. – Martin Ueding Nov 14 '17 at 11:43
  • Your idx value doesn't vary for each day of the week. – gurvinder372 Nov 14 '17 at 11:47
  • My `idx` value *is* the day of the week. Perhaps the misunterstanding is this: I want to generate five pseudo-random numbers that are stable within a week. These five numbers are meant for the five working days in the week. In the next work-week, I want five new numbers. – Martin Ueding Nov 14 '17 at 11:49
  • Your comment above is a much better description of a problem than your question. If you want a new number every day then use `now` instead of `begin_of_week` in your calculation. – gurvinder372 Nov 14 '17 at 12:28