Getting random time interval in postgreSQL












1















I need random interval time between 0 and (10 days and 5 hours).



My code:



select random() * (interval '10 days 5 hours')
from generate_series(1, 50)


It works like should, except a few strange results, like:



0 years 0 mons 7 days 26 hours 10 mins 1.353353 secs


The problem is 26 hours, it shouldn't be more than 23. And I never get 10 days, what I'd like to.










share|improve this question

























  • How do you plan to use this series? Are you planning to use it with dates or timestamps?

    – Tim Biegeleisen
    Dec 31 '18 at 14:59











  • I plan use it with timestamps

    – Jozef
    Dec 31 '18 at 15:03
















1















I need random interval time between 0 and (10 days and 5 hours).



My code:



select random() * (interval '10 days 5 hours')
from generate_series(1, 50)


It works like should, except a few strange results, like:



0 years 0 mons 7 days 26 hours 10 mins 1.353353 secs


The problem is 26 hours, it shouldn't be more than 23. And I never get 10 days, what I'd like to.










share|improve this question

























  • How do you plan to use this series? Are you planning to use it with dates or timestamps?

    – Tim Biegeleisen
    Dec 31 '18 at 14:59











  • I plan use it with timestamps

    – Jozef
    Dec 31 '18 at 15:03














1












1








1








I need random interval time between 0 and (10 days and 5 hours).



My code:



select random() * (interval '10 days 5 hours')
from generate_series(1, 50)


It works like should, except a few strange results, like:



0 years 0 mons 7 days 26 hours 10 mins 1.353353 secs


The problem is 26 hours, it shouldn't be more than 23. And I never get 10 days, what I'd like to.










share|improve this question
















I need random interval time between 0 and (10 days and 5 hours).



My code:



select random() * (interval '10 days 5 hours')
from generate_series(1, 50)


It works like should, except a few strange results, like:



0 years 0 mons 7 days 26 hours 10 mins 1.353353 secs


The problem is 26 hours, it shouldn't be more than 23. And I never get 10 days, what I'd like to.







sql postgresql intervals






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 15:15









marc_s

576k12911111258




576k12911111258










asked Dec 31 '18 at 14:52









JozefJozef

132




132













  • How do you plan to use this series? Are you planning to use it with dates or timestamps?

    – Tim Biegeleisen
    Dec 31 '18 at 14:59











  • I plan use it with timestamps

    – Jozef
    Dec 31 '18 at 15:03



















  • How do you plan to use this series? Are you planning to use it with dates or timestamps?

    – Tim Biegeleisen
    Dec 31 '18 at 14:59











  • I plan use it with timestamps

    – Jozef
    Dec 31 '18 at 15:03

















How do you plan to use this series? Are you planning to use it with dates or timestamps?

– Tim Biegeleisen
Dec 31 '18 at 14:59





How do you plan to use this series? Are you planning to use it with dates or timestamps?

– Tim Biegeleisen
Dec 31 '18 at 14:59













I plan use it with timestamps

– Jozef
Dec 31 '18 at 15:03





I plan use it with timestamps

– Jozef
Dec 31 '18 at 15:03












3 Answers
3






active

oldest

votes


















3














Intervals in Postgres are quite flexible, so hour values of greater than 23 do not necessarily roll over to days. Use jusify_interval() to return them to the normal "days" and "hours"."



So:



select justify_interval(random() * interval '10 day 5 hour')
from generate_series(1, 200)
order by 1 desc;


will return values with appropriate values for days, hours, minutes, and seconds.



Now, why aren't you getting intervals with more than 10 days? This is simple randomness. If you increase the number of rows to 200 (as above), you'll see them (in all likelihood). If you run the code multiple times, sometimes you'll see none in that range; sometimes you'll see two.



Why? You are asking how often you get a value of 240+ in a range of 245. Those top 5 hours account for 0.02% of the range (about 1/50). In other words a sample of 50 is not big enough -- any given sample of 50 random values is likely to be missing 1 or more 5 hour ranges.



Plus, without justify_interval(), you are likely to miss those anyway because they may show up as 9 days with an hours component larger than 23.






share|improve this answer


























  • This is a really great answer, maybe a +15 at some point. You should stay away from Times Square tonight if possible.

    – Tim Biegeleisen
    Dec 31 '18 at 15:23













  • @TimBiegeleisen . . . Happy New Year!

    – Gordon Linoff
    Dec 31 '18 at 15:56



















2














Try this:



select justify_hours(random() * (interval '245 hours'))
FROM generate_series(1, 50)


See Postgres Documentation for an explanation of the justify_* functions.






share|improve this answer

































    1














    One option would be to use an interval of one hour, and then multiply by the random number between 0 and 1 coming from the series:



    select random() * 245 * interval '1 hour'
    from generate_series(1, 50);


    I can see that the other answers suggest using justify_interval. If you just want a series of intervals between 0 and 245 hours (245 hours corresponding to 10 days and 5 hours), then my answer should suffice.






    share|improve this answer


























    • The upvote is really curious. This definitely does not address the concern about hours being greater than 23, not does it explain why intervals greater than 10 days might not be seen.

      – Gordon Linoff
      Dec 31 '18 at 15:12











    • @GordonLinoff I just ran my query and in fact generated one data point with 242 hours, which is greater than 10 days, so I don't really know what you're talking about. Regarding the hours being "greater" than 23, this should not matter assuming the OP is just going to use these values to offset a timestamp. I mean, adding interval 48 hours should be the same as interval 2 days, right?

      – Tim Biegeleisen
      Dec 31 '18 at 15:14











    • . . My comment was directed toward the upvote, not the answer. Whether relevant or not, the OP is specifically asking about hour values greater than 23 -- in fact, probably the reason the OP misses the 10+ days is because of how Postgres represents intervals.

      – Gordon Linoff
      Dec 31 '18 at 15:16











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988729%2fgetting-random-time-interval-in-postgresql%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Intervals in Postgres are quite flexible, so hour values of greater than 23 do not necessarily roll over to days. Use jusify_interval() to return them to the normal "days" and "hours"."



    So:



    select justify_interval(random() * interval '10 day 5 hour')
    from generate_series(1, 200)
    order by 1 desc;


    will return values with appropriate values for days, hours, minutes, and seconds.



    Now, why aren't you getting intervals with more than 10 days? This is simple randomness. If you increase the number of rows to 200 (as above), you'll see them (in all likelihood). If you run the code multiple times, sometimes you'll see none in that range; sometimes you'll see two.



    Why? You are asking how often you get a value of 240+ in a range of 245. Those top 5 hours account for 0.02% of the range (about 1/50). In other words a sample of 50 is not big enough -- any given sample of 50 random values is likely to be missing 1 or more 5 hour ranges.



    Plus, without justify_interval(), you are likely to miss those anyway because they may show up as 9 days with an hours component larger than 23.






    share|improve this answer


























    • This is a really great answer, maybe a +15 at some point. You should stay away from Times Square tonight if possible.

      – Tim Biegeleisen
      Dec 31 '18 at 15:23













    • @TimBiegeleisen . . . Happy New Year!

      – Gordon Linoff
      Dec 31 '18 at 15:56
















    3














    Intervals in Postgres are quite flexible, so hour values of greater than 23 do not necessarily roll over to days. Use jusify_interval() to return them to the normal "days" and "hours"."



    So:



    select justify_interval(random() * interval '10 day 5 hour')
    from generate_series(1, 200)
    order by 1 desc;


    will return values with appropriate values for days, hours, minutes, and seconds.



    Now, why aren't you getting intervals with more than 10 days? This is simple randomness. If you increase the number of rows to 200 (as above), you'll see them (in all likelihood). If you run the code multiple times, sometimes you'll see none in that range; sometimes you'll see two.



    Why? You are asking how often you get a value of 240+ in a range of 245. Those top 5 hours account for 0.02% of the range (about 1/50). In other words a sample of 50 is not big enough -- any given sample of 50 random values is likely to be missing 1 or more 5 hour ranges.



    Plus, without justify_interval(), you are likely to miss those anyway because they may show up as 9 days with an hours component larger than 23.






    share|improve this answer


























    • This is a really great answer, maybe a +15 at some point. You should stay away from Times Square tonight if possible.

      – Tim Biegeleisen
      Dec 31 '18 at 15:23













    • @TimBiegeleisen . . . Happy New Year!

      – Gordon Linoff
      Dec 31 '18 at 15:56














    3












    3








    3







    Intervals in Postgres are quite flexible, so hour values of greater than 23 do not necessarily roll over to days. Use jusify_interval() to return them to the normal "days" and "hours"."



    So:



    select justify_interval(random() * interval '10 day 5 hour')
    from generate_series(1, 200)
    order by 1 desc;


    will return values with appropriate values for days, hours, minutes, and seconds.



    Now, why aren't you getting intervals with more than 10 days? This is simple randomness. If you increase the number of rows to 200 (as above), you'll see them (in all likelihood). If you run the code multiple times, sometimes you'll see none in that range; sometimes you'll see two.



    Why? You are asking how often you get a value of 240+ in a range of 245. Those top 5 hours account for 0.02% of the range (about 1/50). In other words a sample of 50 is not big enough -- any given sample of 50 random values is likely to be missing 1 or more 5 hour ranges.



    Plus, without justify_interval(), you are likely to miss those anyway because they may show up as 9 days with an hours component larger than 23.






    share|improve this answer















    Intervals in Postgres are quite flexible, so hour values of greater than 23 do not necessarily roll over to days. Use jusify_interval() to return them to the normal "days" and "hours"."



    So:



    select justify_interval(random() * interval '10 day 5 hour')
    from generate_series(1, 200)
    order by 1 desc;


    will return values with appropriate values for days, hours, minutes, and seconds.



    Now, why aren't you getting intervals with more than 10 days? This is simple randomness. If you increase the number of rows to 200 (as above), you'll see them (in all likelihood). If you run the code multiple times, sometimes you'll see none in that range; sometimes you'll see two.



    Why? You are asking how often you get a value of 240+ in a range of 245. Those top 5 hours account for 0.02% of the range (about 1/50). In other words a sample of 50 is not big enough -- any given sample of 50 random values is likely to be missing 1 or more 5 hour ranges.



    Plus, without justify_interval(), you are likely to miss those anyway because they may show up as 9 days with an hours component larger than 23.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 31 '18 at 15:22

























    answered Dec 31 '18 at 15:05









    Gordon LinoffGordon Linoff

    773k35306408




    773k35306408













    • This is a really great answer, maybe a +15 at some point. You should stay away from Times Square tonight if possible.

      – Tim Biegeleisen
      Dec 31 '18 at 15:23













    • @TimBiegeleisen . . . Happy New Year!

      – Gordon Linoff
      Dec 31 '18 at 15:56



















    • This is a really great answer, maybe a +15 at some point. You should stay away from Times Square tonight if possible.

      – Tim Biegeleisen
      Dec 31 '18 at 15:23













    • @TimBiegeleisen . . . Happy New Year!

      – Gordon Linoff
      Dec 31 '18 at 15:56

















    This is a really great answer, maybe a +15 at some point. You should stay away from Times Square tonight if possible.

    – Tim Biegeleisen
    Dec 31 '18 at 15:23







    This is a really great answer, maybe a +15 at some point. You should stay away from Times Square tonight if possible.

    – Tim Biegeleisen
    Dec 31 '18 at 15:23















    @TimBiegeleisen . . . Happy New Year!

    – Gordon Linoff
    Dec 31 '18 at 15:56





    @TimBiegeleisen . . . Happy New Year!

    – Gordon Linoff
    Dec 31 '18 at 15:56













    2














    Try this:



    select justify_hours(random() * (interval '245 hours'))
    FROM generate_series(1, 50)


    See Postgres Documentation for an explanation of the justify_* functions.






    share|improve this answer






























      2














      Try this:



      select justify_hours(random() * (interval '245 hours'))
      FROM generate_series(1, 50)


      See Postgres Documentation for an explanation of the justify_* functions.






      share|improve this answer




























        2












        2








        2







        Try this:



        select justify_hours(random() * (interval '245 hours'))
        FROM generate_series(1, 50)


        See Postgres Documentation for an explanation of the justify_* functions.






        share|improve this answer















        Try this:



        select justify_hours(random() * (interval '245 hours'))
        FROM generate_series(1, 50)


        See Postgres Documentation for an explanation of the justify_* functions.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 31 '18 at 15:17

























        answered Dec 31 '18 at 15:09









        clampclamp

        18115




        18115























            1














            One option would be to use an interval of one hour, and then multiply by the random number between 0 and 1 coming from the series:



            select random() * 245 * interval '1 hour'
            from generate_series(1, 50);


            I can see that the other answers suggest using justify_interval. If you just want a series of intervals between 0 and 245 hours (245 hours corresponding to 10 days and 5 hours), then my answer should suffice.






            share|improve this answer


























            • The upvote is really curious. This definitely does not address the concern about hours being greater than 23, not does it explain why intervals greater than 10 days might not be seen.

              – Gordon Linoff
              Dec 31 '18 at 15:12











            • @GordonLinoff I just ran my query and in fact generated one data point with 242 hours, which is greater than 10 days, so I don't really know what you're talking about. Regarding the hours being "greater" than 23, this should not matter assuming the OP is just going to use these values to offset a timestamp. I mean, adding interval 48 hours should be the same as interval 2 days, right?

              – Tim Biegeleisen
              Dec 31 '18 at 15:14











            • . . My comment was directed toward the upvote, not the answer. Whether relevant or not, the OP is specifically asking about hour values greater than 23 -- in fact, probably the reason the OP misses the 10+ days is because of how Postgres represents intervals.

              – Gordon Linoff
              Dec 31 '18 at 15:16
















            1














            One option would be to use an interval of one hour, and then multiply by the random number between 0 and 1 coming from the series:



            select random() * 245 * interval '1 hour'
            from generate_series(1, 50);


            I can see that the other answers suggest using justify_interval. If you just want a series of intervals between 0 and 245 hours (245 hours corresponding to 10 days and 5 hours), then my answer should suffice.






            share|improve this answer


























            • The upvote is really curious. This definitely does not address the concern about hours being greater than 23, not does it explain why intervals greater than 10 days might not be seen.

              – Gordon Linoff
              Dec 31 '18 at 15:12











            • @GordonLinoff I just ran my query and in fact generated one data point with 242 hours, which is greater than 10 days, so I don't really know what you're talking about. Regarding the hours being "greater" than 23, this should not matter assuming the OP is just going to use these values to offset a timestamp. I mean, adding interval 48 hours should be the same as interval 2 days, right?

              – Tim Biegeleisen
              Dec 31 '18 at 15:14











            • . . My comment was directed toward the upvote, not the answer. Whether relevant or not, the OP is specifically asking about hour values greater than 23 -- in fact, probably the reason the OP misses the 10+ days is because of how Postgres represents intervals.

              – Gordon Linoff
              Dec 31 '18 at 15:16














            1












            1








            1







            One option would be to use an interval of one hour, and then multiply by the random number between 0 and 1 coming from the series:



            select random() * 245 * interval '1 hour'
            from generate_series(1, 50);


            I can see that the other answers suggest using justify_interval. If you just want a series of intervals between 0 and 245 hours (245 hours corresponding to 10 days and 5 hours), then my answer should suffice.






            share|improve this answer















            One option would be to use an interval of one hour, and then multiply by the random number between 0 and 1 coming from the series:



            select random() * 245 * interval '1 hour'
            from generate_series(1, 50);


            I can see that the other answers suggest using justify_interval. If you just want a series of intervals between 0 and 245 hours (245 hours corresponding to 10 days and 5 hours), then my answer should suffice.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 31 '18 at 15:12

























            answered Dec 31 '18 at 15:07









            Tim BiegeleisenTim Biegeleisen

            225k1391143




            225k1391143













            • The upvote is really curious. This definitely does not address the concern about hours being greater than 23, not does it explain why intervals greater than 10 days might not be seen.

              – Gordon Linoff
              Dec 31 '18 at 15:12











            • @GordonLinoff I just ran my query and in fact generated one data point with 242 hours, which is greater than 10 days, so I don't really know what you're talking about. Regarding the hours being "greater" than 23, this should not matter assuming the OP is just going to use these values to offset a timestamp. I mean, adding interval 48 hours should be the same as interval 2 days, right?

              – Tim Biegeleisen
              Dec 31 '18 at 15:14











            • . . My comment was directed toward the upvote, not the answer. Whether relevant or not, the OP is specifically asking about hour values greater than 23 -- in fact, probably the reason the OP misses the 10+ days is because of how Postgres represents intervals.

              – Gordon Linoff
              Dec 31 '18 at 15:16



















            • The upvote is really curious. This definitely does not address the concern about hours being greater than 23, not does it explain why intervals greater than 10 days might not be seen.

              – Gordon Linoff
              Dec 31 '18 at 15:12











            • @GordonLinoff I just ran my query and in fact generated one data point with 242 hours, which is greater than 10 days, so I don't really know what you're talking about. Regarding the hours being "greater" than 23, this should not matter assuming the OP is just going to use these values to offset a timestamp. I mean, adding interval 48 hours should be the same as interval 2 days, right?

              – Tim Biegeleisen
              Dec 31 '18 at 15:14











            • . . My comment was directed toward the upvote, not the answer. Whether relevant or not, the OP is specifically asking about hour values greater than 23 -- in fact, probably the reason the OP misses the 10+ days is because of how Postgres represents intervals.

              – Gordon Linoff
              Dec 31 '18 at 15:16

















            The upvote is really curious. This definitely does not address the concern about hours being greater than 23, not does it explain why intervals greater than 10 days might not be seen.

            – Gordon Linoff
            Dec 31 '18 at 15:12





            The upvote is really curious. This definitely does not address the concern about hours being greater than 23, not does it explain why intervals greater than 10 days might not be seen.

            – Gordon Linoff
            Dec 31 '18 at 15:12













            @GordonLinoff I just ran my query and in fact generated one data point with 242 hours, which is greater than 10 days, so I don't really know what you're talking about. Regarding the hours being "greater" than 23, this should not matter assuming the OP is just going to use these values to offset a timestamp. I mean, adding interval 48 hours should be the same as interval 2 days, right?

            – Tim Biegeleisen
            Dec 31 '18 at 15:14





            @GordonLinoff I just ran my query and in fact generated one data point with 242 hours, which is greater than 10 days, so I don't really know what you're talking about. Regarding the hours being "greater" than 23, this should not matter assuming the OP is just going to use these values to offset a timestamp. I mean, adding interval 48 hours should be the same as interval 2 days, right?

            – Tim Biegeleisen
            Dec 31 '18 at 15:14













            . . My comment was directed toward the upvote, not the answer. Whether relevant or not, the OP is specifically asking about hour values greater than 23 -- in fact, probably the reason the OP misses the 10+ days is because of how Postgres represents intervals.

            – Gordon Linoff
            Dec 31 '18 at 15:16





            . . My comment was directed toward the upvote, not the answer. Whether relevant or not, the OP is specifically asking about hour values greater than 23 -- in fact, probably the reason the OP misses the 10+ days is because of how Postgres represents intervals.

            – Gordon Linoff
            Dec 31 '18 at 15:16


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988729%2fgetting-random-time-interval-in-postgresql%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas