Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt and random numbers

    General and Desktop
    7
    11
    16880
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      tucnak last edited by

      Hi, Qt Developer Network!

      My project have to create random "Shultz tables":http://www.st.vstour.info/ShultzTables.html

      So it is my first try with qrand() and qsrand().

      Can you help me and write howto write a code that will generate random number in diapason 1-25?

      Thanks!

      <body ></body>

      1 Reply Last reply Reply Quote 0
      • V
        veeeee_d last edited by

        http://doc.qt.nokia.com/latest/qtglobal.html#qrand

        1 Reply Last reply Reply Quote 0
        • T
          tucnak last edited by

          I know it. I want to generate numbers in some diapason with special seed.

          1 Reply Last reply Reply Quote 0
          • D
            dangelog last edited by

            I'm not sure what that means (special seed?). Could you elaborate a little bit?

            Software Engineer
            KDAB (UK) Ltd., a KDAB Group company

            1 Reply Last reply Reply Quote 0
            • F
              fluca1978 last edited by

              Not sure what you need, I guess a random number between 1 and 25. I don't know if there is a way to limit qrand (different from MAX_RAND), but maybe you can iterate over qrand to get a limited value?

              1 Reply Last reply Reply Quote 0
              • C
                cincirin last edited by

                [quote author="fluca1978" date="1320931836"]... but maybe you can iterate over qrand to get a limited value?[/quote]
                In this situation the best method, in my opinion is:
                @
                qrand() % 25 + 1
                @

                1 Reply Last reply Reply Quote 0
                • V
                  veeeee_d last edited by

                  Well I thought that solution was implied, my bad.

                  1 Reply Last reply Reply Quote 0
                  • A
                    ardhitama last edited by

                    You just need to implement your own nSeeder, try to start from getting current time's second.

                    @
                    qsrand(nSeeder);
                    inline int rangedRand(unsigned int min, unsigned int max){
                    return (qrand() % (max-min)+1) + min;
                    }
                    @

                    Love automagically stuffs

                    1 Reply Last reply Reply Quote 0
                    • F
                      fluca1978 last edited by

                      Good solution!

                      1 Reply Last reply Reply Quote 0
                      • R
                        rentner323 last edited by

                        Sorry for my post into such an old thread, but it must be:

                        @inline int rangedRand(unsigned int min, unsigned int max){
                        return (qrand() % (max+1-min)) + min;
                        }@

                        1 Reply Last reply Reply Quote 0
                        • A
                          ardhitama last edited by

                          [quote author="rentner323" date="1410941949"]Sorry for my post into such an old thread, but it must be:

                          @inline int rangedRand(unsigned int min, unsigned int max){
                          return (qrand() % (max+1-min)) + min;
                          }@[/quote]

                          Correct, curse me for the bracket

                          Love automagically stuffs

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post