Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt and random numbers
QtWS25 Last Chance

Qt and random numbers

Scheduled Pinned Locked Moved General and Desktop
11 Posts 7 Posters 17.3k Views
  • 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 Offline
    T Offline
    tucnak
    wrote on last edited by
    #1

    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
    0
    • V Offline
      V Offline
      veeeee_d
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tucnak
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

          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
          0
          • F Offline
            F Offline
            fluca1978
            wrote on last edited by
            #5

            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
            0
            • C Offline
              C Offline
              cincirin
              wrote on last edited by
              #6

              [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
              0
              • V Offline
                V Offline
                veeeee_d
                wrote on last edited by
                #7

                Well I thought that solution was implied, my bad.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  ardhitama
                  wrote on last edited by
                  #8

                  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
                  0
                  • F Offline
                    F Offline
                    fluca1978
                    wrote on last edited by
                    #9

                    Good solution!

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      rentner323
                      wrote on last edited by
                      #10

                      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
                      0
                      • A Offline
                        A Offline
                        ardhitama
                        wrote on last edited by
                        #11

                        [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
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved