Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. A small problem with qRand().
Forum Updated to NodeBB v4.3 + New Features

A small problem with qRand().

Scheduled Pinned Locked Moved Game Development
11 Posts 8 Posters 9.0k Views 1 Watching
  • 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.
  • O Offline
    O Offline
    Ouss4
    wrote on last edited by
    #1

    Hi everybody,

    This is my first post, so it's going to be simple ;)

    Well, in one of my app, I need to generate a big random number, the problem is qRand() can not pass RAND_MAX ( equal mostly to 2^15 ).

    So, my question is : how can I generate a number greater than RAND_MAX with the Qt way ?

    Before leaving I thank you for all possible answers.

    Talking has worth thin...

    1 Reply Last reply
    0
    • T Offline
      T Offline
      task_struct
      wrote on last edited by
      #2

      May be you can use "qHash":http://doc.trolltech.com/4.8-snapshot/qhash.html#related-non-members with "qint64 QDateTime::currentMSecsSinceEpoch ":http://doc.trolltech.com/4.8-snapshot/qdatetime.html#currentMSecsSinceEpoch as input or

      @ qRand() * QDateTime::currentMSecsSinceEpoch() @

      "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program."

      • Linu...
      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        You could try to generate two or three random numbers, multiply the first two and optionally add the third to it, or use some other way of combining the results (bitshifting and or-ing comes to mind).

        1 Reply Last reply
        0
        • O Offline
          O Offline
          Ouss4
          wrote on last edited by
          #4

          First of all, I thank both of you for replying.

          Andre : generating a random number and multiply/add it with another one, will reduce the equiprobability, that's why I avoided this solution.

          task_struct : I realy don't know how to use QHach to generate a random number.
          and for :
          @qRand() * QDateTime::currentMSecsSinceEpoch()@
          I think it doesn't make any difference because qRand() won't pass RAND_MAX.
          I'll try and let you know.

          Talking has worth thin...

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            @Ouss4: yes, you are right. I am not sure if the bitshift trick would prevent that. At first sight, I'd say it does, but I might be off there.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #6

              May I ask for the purpose for such large random numbers? Is it about generating unqiue identifiers? Is it about statistics (as you menitoned equiprobability)? Or is it about cryptograpy? Probably qRand() isn't sufficient at all for your use case.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                [quote author="Andre" date="1318432218"]@Ouss4: yes, you are right. I am not sure if the bitshift trick would prevent that. At first sight, I'd say it does, but I might be off there.[/quote]

                AFAIK bitshifting should do the trick:

                @
                int randNumber = (qRand() << 16) + qRand();
                @

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  Ouss4
                  wrote on last edited by
                  #8

                  Thanks guys, I'm going to try with bitshifting.

                  Talking has worth thin...

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jasonvin
                    wrote on last edited by
                    #9

                    Thanks
                    [quote author="Gerolf" date="1318433239"][quote author="Andre" date="1318432218"]@Ouss4: yes, you are right. I am not sure if the bitshift trick would prevent that. At first sight, I'd say it does, but I might be off there.[/quote]

                    AFAIK bitshifting should do the trick:

                    @
                    int randNumber = (qRand() << 16) + qRand();
                    @
                    [/quote]

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      matti-
                      wrote on last edited by
                      #10

                      Or you could go the portable way and use srand() / rand()?

                      Author of <a href="http://mmark.777-team.org/">MMark13</a>

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

                        Which OS are you using? Under any 32-bit Linux I have access RAND_MAX is 2^32 - 1.

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

                        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