Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By jim829

Thread: Generating random hours and minutes in String

  1. #1
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default [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[10];
    
            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[10] ). 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default 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.
    ghandil likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default 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
    ghandil likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default 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;
            }

Similar Threads

  1. Generating random sentences
    By mayank1 in forum New To Java
    Replies: 5
    Last Post: 11-03-2014, 11:27 PM
  2. Generating random numbers.
    By Zarquad in forum New To Java
    Replies: 1
    Last Post: 07-25-2013, 11:31 PM
  3. Turn seconds to hours/minutes/sec
    By Scruss in forum New To Java
    Replies: 6
    Last Post: 09-06-2011, 06:48 PM
  4. getting hours and minutes
    By silversurfer2in in forum New To Java
    Replies: 5
    Last Post: 02-17-2010, 07:35 AM
  5. Generating a random number
    By oridov in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 05:12 PM

Posting Permissions

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