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. Random numbers in Qt
Qt 6.11 is out! See what's new in the release blog

Random numbers in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 6.0k Views 3 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.
  • M Offline
    M Offline
    Mimoid
    wrote on last edited by A Former User
    #1

    Hi! I want random number for a few thousands numbers range, but i dont know how to do it.
    This is my code and this gives me each program run the same combination of numbers. For example 2,7,5,9... And next time when i run program is the same sequence...

    int number=tab.size(); //for now size of my array is 1511
        random = qrand() % number;
    

    This is code of the same program but in console and it works good.

    srand(time(NULL));
        random=rand()%tab.size();
    

    It doesnt need to be super perfect random, but although has a different order in each run.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! You forgot to initialize the prng. See: void qsrand(uint seed).

      1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Just add qsrand and you should be good to go.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • Paul ColbyP Offline
          Paul ColbyP Offline
          Paul Colby
          wrote on last edited by
          #4

          Hi @Mimoid,

          Your console program works as you want because you're seeding the rand function via the srand call. The Qt equivalent (thread-safe wrapper) is qsrand. So try something like:

          qsrand(time(NULL));
          ...
          random = qrand() % number;
          

          Cheers.

          1 Reply Last reply
          2
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Well... 3 times the same answer :D

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            ? 1 Reply Last reply
            3
            • SGaistS SGaist

              Well... 3 times the same answer :D

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              @SGaist Must be correct then :)

              1 Reply Last reply
              3
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #7

                I'll give a different answer then, use C++11 random. already thread safe and you can generate random numbers according to different probability distribution:
                http://www.cplusplus.com/reference/random/

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @VRonin you rebel ! ;) :D

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  • Login

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