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 random number
QtWS25 Last Chance

qt random number

Scheduled Pinned Locked Moved Unsolved General and Desktop
randomqt5.5.0
9 Posts 4 Posters 32.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.
  • L Offline
    L Offline
    Lorence
    wrote on last edited by
    #1

    in C++
    srand(time(NULL));
    rand()%2+0; // generates random 1 and 0

    how can i write this in QT? when i use qsrand and qrand i always get the same random numbers everytime the program is started.

    i want to use QT for random generation so it is all interpreted the same on different platforms

    K 1 Reply Last reply
    0
    • L Lorence

      in C++
      srand(time(NULL));
      rand()%2+0; // generates random 1 and 0

      how can i write this in QT? when i use qsrand and qrand i always get the same random numbers everytime the program is started.

      i want to use QT for random generation so it is all interpreted the same on different platforms

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Lorence
      How do you set the seed?
      Respectively, what are you using for setting the seed?

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mjsurette
        wrote on last edited by
        #3

        The c++11 random number generator is random, not pseudo random and unless you are running an embedded system should be available.

        kshegunovK 1 Reply Last reply
        0
        • M mjsurette

          The c++11 random number generator is random, not pseudo random and unless you are running an embedded system should be available.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @mjsurette
          This is news to me, could you provide a verifiable source for that statement? Maybe I'm not up to date, but as far as I know in C++11 you still seed the sequence, which would mean you have pseudo-random generation.

          Read and abide by the Qt Code of Conduct

          M 1 Reply Last reply
          0
          • L Offline
            L Offline
            Lorence
            wrote on last edited by
            #5

            this is how i write it in qt

            qsrand(0);
            qrand() % 2 + 0;

            kshegunovK K 2 Replies Last reply
            0
            • L Lorence

              this is how i write it in qt

              qsrand(0);
              qrand() % 2 + 0;

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              @Lorence
              You have to use a different seed every time you start your program to get a different sequence of numbers. Otherwise you'll get the same sequence (hence the name: pseudo-random number generator). You could use something like this to initialize your sequence:

              // Initialize sequence
              qsrand(QDateTime::currentMSecsSinceEpoch() / 1000);
              // ... Generate numbers from that sequence
              qrand();
              // ...
              qrand();
              // ...
              

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • kshegunovK kshegunov

                @mjsurette
                This is news to me, could you provide a verifiable source for that statement? Maybe I'm not up to date, but as far as I know in C++11 you still seed the sequence, which would mean you have pseudo-random generation.

                M Offline
                M Offline
                mjsurette
                wrote on last edited by
                #7

                @kshegunov
                http://www.cplusplus.com/reference/random/
                Check out random_device.

                kshegunovK 1 Reply Last reply
                0
                • L Lorence

                  this is how i write it in qt

                  qsrand(0);
                  qrand() % 2 + 0;

                  K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  @Lorence said:

                  this is how i write it in qt

                  qsrand(0);
                  qrand() % 2 + 0;

                  Not much to add to the other responses.
                  In your C++ example you are using the current time as seed. Since there is between different trials always another time you will have everytime also another sequence of pseudo-random numbers.

                  In your Qt example above you are using always '0' as a seed and therefore you will always get the identical sequence of pseudo-random numbers.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • M mjsurette

                    @kshegunov
                    http://www.cplusplus.com/reference/random/
                    Check out random_device.

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by kshegunov
                    #9

                    @mjsurette
                    With the important note:

                    Notice that random devices may not always be available to produce random numbers (and in some systems, they may even never be available). This is signaled by throwing an exception derived from the standard exception on construction or when a number is requested with operator().
                    Unless the program really requires a stochastic process to generate random numbers, a portable program is encouraged to use an alternate pseudo-random number generator engine instead, or at least provide a recovery method for such exceptions.

                    Windows will not provide real random numbers (at least to my knowledge), and in Linux this is implementation dependent. Even if /dev/random is advertised to produce real random numbers, the truthfulness of such a statement is debatable. Often enough the random device just generates a pseudo-random sequence. That's why you get the above note on portability.

                    Kind regards.

                    Read and abide by the Qt Code of Conduct

                    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