0

I am looking for solution in Swift 3 where my app will generate random number from current date in the range <0;1000>.

For example I would love to get something like this:

14/2/2017 -> 323
15/2/2017 -> 25
16/2/2017 -> 898
...

If I remember right in Java I was able to generate same number each time by using certain seed in my random number generator which allowed me to generate same number all the time. I was thinking about using value of year+month+day as the seed to generate same number for all the users that will use app at the same day.

Java:

seed = currentYear + currentMonth + currentDay
Random generator = new Random(seed);
double random = generator.nextDouble();
...

Please help me to solve that problem in Swift 3, I have tried to go through all arc4random and other random functions and I didn't found any solution.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ondřej Ševčík
  • 1,049
  • 3
  • 15
  • 31
  • If you just add the month, day, and year you are going to get the same seeds repeating 11/2/2017 and 12/1/2017 would have the same seed, for example. I suggest using `year*365+month*31+day` as the seed to `srand48()` – Duncan C Feb 14 '17 at 23:36
  • 1
    Actually it should be `year * 372 + month * 31 + day` to get unique seeds, as we assuming that every month have maximum 31 days, then 31 * 12 = **372**. – Volodymyr Kelembet Feb 15 '17 at 00:53
  • So you may end up with solution similar to the following [code example](https://gist.github.com/vkelembet/3c4e10361f7511b408a29ec44cf590fe) – Volodymyr Kelembet Feb 15 '17 at 01:36
  • Thanks for code example Volodymyr! I have used it in my project and it works exactly as I wanted. – Ondřej Ševčík Feb 16 '17 at 10:09

0 Answers0