# Thread: Generating random hours and minutes in String

1. Member Join Date
Jun 2013
Posts
28
Rep Power
0

## [SOLVED] Generating random hours and minutes in String

Hi,

I want a method to make random times (save them to a String array and return the array). The hours would be between 6 and 9 (AM) and the minutes are thoroughly random which will be added to the hour. This is the body of the method I got so far:

Java Code:
```        double random = Math.random();
DecimalFormat df = new DecimalFormat("#.##");
String minutes = df.format(random);
String firstHour;
String[] times = new String;

if (0 <= random && random <= 0.3) {
firstHour = "06";
} else if (0.3 < random && random <= 0.6) {
firstHour = "07";
} else if (0.6 < random && random <= 0.9) {
firstHour = "08";
} else {
firstHour = "09";
}
int hour = Integer.parseInt(firstHour);
int counter = 0;
while (hour <= 23) {
times[counter] = hour + ":" + minutes;
counter++;
hour += 2;
}```
I have a problem with the minute part. what's the pattern for DecimalFormat to get only the numbers after decimal point (e.g. extracting 59 from 0.59) The bigger issue though is how to convert it that would be true in the context of minutes. (As Math.random() generates numbers between 0 to 1 which might be for example 0.84 that the extracted num (84) definitely is a wrong number for a minute.

[Run and test the above code online]

By the way, Any suggestion for the dynamic size of the string array, times? ( String[] times = new String ). It's not crucial to my program though. Is the only way using ArrayList?

Thanks
Last edited by ghandil; 03-02-2016 at 11:42 PM.  Reply With Quote

2. ## Re: Generating random hours and minutes in String

extracting 59 from 0.59
Where is the value: 0.59? In a String or is it a float or double? If in a String the substring method could be used to get the digits after the decimal point.
If in a float or double a way would be to multiply it by 100: .59 * 100 = 59

Math.random() generates numbers between 0 to 1
Look at the Random class's nextInt(bound) method.
Last edited by Norm; 03-02-2016 at 10:53 PM.  Reply With Quote

3. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: Generating random hours and minutes in String

Must you use floating point math to do this? Why not use the Random class and generate integer values for the hours and minutes directly? You can then easily convert them to a formatted String.

Regards,
Jim  Reply With Quote

4. Member Join Date
Jun 2013
Posts
28
Rep Power
0

## Re: Generating random hours and minutes in String

Thanks dear Norm and dear Jim :)

I haven't seen Random class and nexInt(bound); now it works very well.

If in a float or double a way would be to multiply it by 100: .59 * 100 = 59
Excellent! sometimes answers are so close that you just can't see them Here's my modified and more efficient code:

Java Code:
```
Random randomnum = new Random();
int random = randomnum.nextInt(60);
String firstHour;
List<String> times = new ArrayList<>();

if (0 <= random && random <= 15) {
firstHour = "06";
} else if (15 < random && random <= 30) {
firstHour = "07";
} else if (30 < random && random <= 45) {
firstHour = "08";
} else {
firstHour = "09";
}

int hour = Integer.parseInt(firstHour);
while (hour <= 23) {
times.add(hour + ":" + random);
hour += 2;
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•