Hot to get a random date in the range

I have two dates as strings (dd-mm-yyyy). How can I get a random date between these two dates?

+3
source share
4 answers

You can convert dates to a Unix timestamp. Randomly select a timestamp between two timestamps and convert it back.

+3
source

Convert dates to fixed numbers such as Julian values ​​(name them J1 and J2). Generate a "random" number from 0 <= N <= J2-j1. Then convert J1 + N from Julian back to date.

+1
source

3 : d1 = dd, m1 = mm, y1 = yyyy, d2, m2, y2

, , (http://www.cprogramming.com/tutorial/random.html), y1 y2, . dd 1 28 1 12. 28. , , , ( , , , , 29 ).

int a b, :

int random = a + rand() % (b - a + 1);

, y1 = y2 m1 = m2,

0
source

Use the types and functions from the standard time.h library:

http://en.wikipedia.org/wiki/Time.h

Convert the string details to int and then convert them to time_t. After calculating your random value between them, go another way to get the result in a row.

0
source

Source: https://habr.com/ru/post/1735255/


All Articles