Decrease code between ifdefs.
Use c++11 facilities to generate a random number.
Use static functions to create/add cookies.

Previous approach was creating multiple cookie objects each generating a
random cookie in the constructor, but never using them.