# (Solved) Generating a random date

Hey,
I’m just wondering how do I generate a random date like 2020. may 20. I tried to generate the year first, that’s okay, but how do I generate a random month (a text)?

Hmm… I’d probably do it this way:

1. Create a structure variable called something like Month
2. Create twelve child variables named 0 through 11
3. For each child variable, give a month as the value. e.g. January
4. Set up an event to call your Month variable, using a random number to call the child variable, and return the text. e.g. something like GlobalVariableString(Month[ToString(random(11))])

Thanks, it was really helpful!..But I have an other question.
Then how do I generate a random day in a random month and year considering that some months have 30 days, some have 31 and february has 29, and additionally, in leap years it has 28?
Uff…it’s so complicated!

That’s a bit more difficult.

For normal months: I would set up some conditions that look at your Month variable, then take action.
e.g.

``````OR statement
If Month variable = "January" |  Set the day variable = "random(30) +1"
If Month variable = "March"   |
If Month variable = "May"     |
etc
``````

Then do the same for 30 day months. but it’d be “random(29)”

For Feburary, you need to have some more advanced calculations. You’d be checking your month AND your year variables.

So it’d be like
Parent event

``````If Month Variable = "February" | (empty)
``````

then a child event

``````If Year Variable = Leap Year algorithm | Random(29)
``````

Then another sub event that is the same as the above, but inverted.

You’ll need to figure out the math for the leap year, the algorithm is listed on Wikipedia: Leap year - Wikipedia

1 Like

OK, so now I’m totally confused…

Firstly, why does it give me back always a 0 to the year and the day?

Secondly, how to check if the Year variable is divisible by another number?
(Sorry I’ve just started to work with GD recently.)

Btw my goal is to make a game where the player has to calculate the day of the week of a randomly generated date and the game checks if it’s correct.

Can’t answer most of your questions, but first thing I would make sure: are your global variables lower case? Because it’s checking for exact matches, so if you’re doing = “january” but you have “January” in your variable, that won’t work.

Secondly, as far as I see, you’re never setting a month variable. You’re setting a text object to the month, and checking a month variable?

What decides the variable “Month”? You’re not setting it anywhere.

1 Like

OR statement
If Month variable = “January” | Set the day variable = “random(30) +1”
If Month variable = “March” | Change the text Day: to ToString(GlobalVariable(Day))
If Month variable = “May” |
etc

That does not work…
If I run that the day is changing continuously between 1 and 31.
Why is that?

You probably need “trigger once” in most of these events, or they need to be sub events of “At the beginning of the scene”

1 Like