I’m trying to generate a random month and year with an upper bound of the current year and month and a lower bound that will be a static month and year.
I can generate the year with:
int randomYear = (int) (Math.random() * ( currentYear - 2000 ))+2000;
//returns a val 2000-2012
Where 2000 is my lower bound. So hypothetically that would generate a year between 2012-2000. The problem I run into is when I try to set a month I’m not sure how to handle if the random generator returns 2012 and there is only 2 months. Currently I have:
int randomMonth = (int) Math.random() * (12);
How do I handle the special cases such as the case with 2012 or if I set the lower bounds for a year 2000 and a month of 10?
This might seems a little bit long to you, but it’s much nicer to work with actual dates:
//edit: puh.. harder than I thought 😉 But that’s it.