It is common to find a random number between 2 given numbers… In our java application that we are working on, We had a need to find a random time between two given timestamp.. We felt it little challenging in the beginning… But later figured out a simple solution for the same… The steps for generating a random time between 2 timestamps really turned out to be very simple & this article is about how we achieved that…

If min and max are the 2 variables that are holding minimum and maximum values, & random is the random number that is to be generated, in java the random number is calculated in this way…

random = (Math.random() * (max - min) ) + min

Here Math.random() is a function in java that generates any random value between 0 & 1… We multiply that with the difference between max & min values and finally add the obtained result with the min value to get a random number between 2 given numbers.. This idea was set as the basement for our requirement too… Wondering how?? This is how it goes…

We use Calendar object which defaults to current date and time. We then set Hour, minute & second in the same object(If required we can set the year, month & date too).. Now we have the Calendar object with the date what we set or current date and time. Calendar class has a method named getTimeInMillis which will convert this timeStamp to a long value. This long value serves as the first min variable as in the example where we generate a random number between 2 different numbers.

Now in the same way we get the long value of the second timestamp too which is the max variable value. Using a similar calculation as above we get a random long number between 2 different long numbers which are the long time. Finally we construct the date object with the long value that we obtained.. This date object’s value is the random time stamp between 2 different Calendar objects and hence the we can generate random date/time between two different Calendar objects!!!! :-)

Here goes the code for that..

//code to generate random timestamp between morning 6 to evening 8 pm
public static void main(String args[]){
Calendar cdr = Calendar.getInstance();
cdr.set(Calendar.HOUR_OF_DAY, 6);
cdr.set(Calendar.MINUTE, 0);
cdr.set(Calendar.SECOND, 0);
long val1=cdr.getTimeInMillis();


cdr.set(Calendar.HOUR_OF_DAY, 20);
cdr.set(Calendar.MINUTE, 0);
cdr.set(Calendar.SECOND, 0);
long val2=cdr.getTimeInMillis();


Random r=new Random();
long randomTS=(long)(r.nextDouble()*(val2-val1))+val1;
Date d=new Date(randomTS);
System.out.println(d.toString());
}

PS: This will work even for generating random date between two different dates which can be a month/year gap.

Posted by Nivasini, filed under Java. Date: July 17, 2008, 7:04 pm |

One Response

  1. maqbool Says:

    Nice work…cleaver use of the calendar object.

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.