Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Problem with Math.random()
Forum Update on Monday, May 27th 2025

Problem with Math.random()

Scheduled Pinned Locked Moved Solved QML and Qt Quick
21 Posts 5 Posters 3.4k 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.
  • M Offline
    M Offline
    MostafaGamal
    wrote on 5 Mar 2020, 20:40 last edited by
    #1

    Hello Everyone,

    need a little help here about using Math.random() in QML

    i'm using the following function to generate unique UUID:

    function uuidv4() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
            var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
          })
    }
    

    it was working perfect using QT 5.12.4 and QT Creator 4.9.1 on Ubuntu 16.04.6
    after a fresh installation of Ubuntu 18.04.4 with same QT & QT Creator versions, it behaved incorrectly !

    Math.random() shall generate new unique number between 0 & 1 including 0
    after debugging the code, the value of Math.random() is always constant = 0.4630094115769047

    i don't know why that happened

    i noticed the following:
    after running the app at the run time, if i called the function many times, looks like the value has changed to another constant value !

    if the app terminated and re-run again Math.random() resets to the main constant again (0.4630094115769047).

    i know it maybe a strange case, but any help would be appreciated.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fcarney
      wrote on 5 Mar 2020, 20:54 last edited by
      #2

      Set the seed value to something that is different every time. Like the system clock.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        MostafaGamal
        wrote on 5 Mar 2020, 20:58 last edited by
        #3

        It was working without any seed before.

        though, i tried what you asked too: Math.random(new Date().getTime())
        still the same constant value

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fcarney
          wrote on 5 Mar 2020, 21:00 last edited by
          #4

          Yeah, I was just looking and there might not be a way to set the seed. You might be better off doing it on the C++ side if the QML one won't work.

          https://doc.qt.io/qt-5/qrandomgenerator.html

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MostafaGamal
            wrote on 5 Mar 2020, 21:10 last edited by
            #5

            would do it, but need a logical reason for not being work correctly for just upgrading my OS !
            as it was working well before.

            besides, i think it is not a good practice to only write this function and call it from C++ and continue my logic in QML, right ?

            J J 2 Replies Last reply 5 Mar 2020, 21:32
            0
            • M MostafaGamal
              5 Mar 2020, 21:10

              would do it, but need a logical reason for not being work correctly for just upgrading my OS !
              as it was working well before.

              besides, i think it is not a good practice to only write this function and call it from C++ and continue my logic in QML, right ?

              J Offline
              J Offline
              JonB
              wrote on 5 Mar 2020, 21:32 last edited by
              #6

              @MostafaGamal
              Maybe you have resurrected this from 2010: https://lists.qt-project.org/pipermail/qt-interest-old/2010-October/028373.html :-)
              Obviously, something is wrong!
              Maybe if they could mess it up once in 2010 they can do it again now ;-)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MostafaGamal
                wrote on 5 Mar 2020, 21:43 last edited by
                #7

                @JonB
                Maybe you are right, but ...

                could this happen with the same version of QT & QT Creator but different OS version ? i don't think so!

                anyway .. still waiting a confirmation from anyone tried it or a mod here.

                @fcarney @JonB
                Thanks guys for your replys :))

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fcarney
                  wrote on 5 Mar 2020, 22:19 last edited by
                  #8

                  Math.random is a Javascript command. There is also no option in Javascript to seed the random number. So all the solutions out there are roll your own.

                  In the long run it would be better to do most things from the C++ side. There is a whole world of difference and performance doing things in C++ versus the JS side.

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MostafaGamal
                    wrote on 5 Mar 2020, 22:26 last edited by
                    #9

                    That's true.
                    i agree with you.

                    but for some reasons .. i can't move on the C++ side for now.

                    but as u said, it is a JS command, so ... is there a missing dependency or OS package that needs to be installed 1st to let JS works well in QT ?

                    maybe there is some QT versions that has a problems with JS ? if so .. which recommended version of QT shall i install alongside QT Creator version ?

                    J 1 Reply Last reply 6 Mar 2020, 06:33
                    0
                    • M MostafaGamal
                      5 Mar 2020, 21:10

                      would do it, but need a logical reason for not being work correctly for just upgrading my OS !
                      as it was working well before.

                      besides, i think it is not a good practice to only write this function and call it from C++ and continue my logic in QML, right ?

                      J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 6 Mar 2020, 02:40 last edited by JKSH 3 Jul 2020, 01:38
                      #10

                      @MostafaGamal said in Problem with Math.random():

                      need a logical reason for not being work correctly for just upgrading my OS !

                      There are many possibilities.

                      Do you have an AMD Ryzen CPU? Some models and BIOS versions have faulty random number generators (RNGs): https://forum.manjaro.org/t/i-get-a-cpu-random-generator-warning-advising-me-to-disable-hardware-random-number-generation/116796

                      • Try updating your BIOS
                      • Run some tests using rng-tools: https://wiki.archlinux.org/index.php/Rng-tools (the article is for Arch Linux but the same tools exist in Ubuntu repos)

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      M 1 Reply Last reply 6 Mar 2020, 15:28
                      3
                      • M MostafaGamal
                        5 Mar 2020, 22:26

                        That's true.
                        i agree with you.

                        but for some reasons .. i can't move on the C++ side for now.

                        but as u said, it is a JS command, so ... is there a missing dependency or OS package that needs to be installed 1st to let JS works well in QT ?

                        maybe there is some QT versions that has a problems with JS ? if so .. which recommended version of QT shall i install alongside QT Creator version ?

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 6 Mar 2020, 06:33 last edited by
                        #11

                        @MostafaGamal said in Problem with Math.random():

                        but for some reasons .. i can't move on the C++ side for now.

                        you should really consider it, because you're reinventing the wheel ;)

                        Qt has a nice and easy to use QUuid class
                        simply wrap it in a basic class and expose it to QML and import it where you need it.


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        M 1 Reply Last reply 6 Mar 2020, 15:51
                        1
                        • J JKSH
                          6 Mar 2020, 02:40

                          @MostafaGamal said in Problem with Math.random():

                          need a logical reason for not being work correctly for just upgrading my OS !

                          There are many possibilities.

                          Do you have an AMD Ryzen CPU? Some models and BIOS versions have faulty random number generators (RNGs): https://forum.manjaro.org/t/i-get-a-cpu-random-generator-warning-advising-me-to-disable-hardware-random-number-generation/116796

                          • Try updating your BIOS
                          • Run some tests using rng-tools: https://wiki.archlinux.org/index.php/Rng-tools (the article is for Arch Linux but the same tools exist in Ubuntu repos)
                          M Offline
                          M Offline
                          MostafaGamal
                          wrote on 6 Mar 2020, 15:28 last edited by
                          #12

                          @JKSH
                          Yes, i have the same processor as in the forum post : AMD Ryzen 7 3700X 8-Core @ 16x 3.6GHz
                          if updating BIOS is the solution ... which BIOS version shall i install ? (My MotherBoard is : Gigabyte B450 Aorus Pro Wifi)

                          also installed rng-tools for testing.
                          that was the output of command

                          rngtest -c 1000 </dev/random
                          

                          rngtest 5
                          Copyright (c) 2004 by Henrique de Moraes Holschuh
                          This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

                          rngtest: starting FIPS tests...
                          rngtest: bits received from input: 20000032
                          rngtest: FIPS 140-2 successes: 999
                          rngtest: FIPS 140-2 failures: 1
                          rngtest: FIPS 140-2(2001-10-10) Monobit: 0
                          rngtest: FIPS 140-2(2001-10-10) Poker: 0
                          rngtest: FIPS 140-2(2001-10-10) Runs: 1
                          rngtest: FIPS 140-2(2001-10-10) Long run: 0
                          rngtest: FIPS 140-2(2001-10-10) Continuous run: 0
                          rngtest: input channel speed: (min=263.549; avg=252860.427; max=6493506.494)bits/s
                          rngtest: FIPS tests speed: (min=45.960; avg=84.689; max=87.094)Mibits/s
                          rngtest: Program run time: 79320378 microseconds

                          also tried this command : sudo rngd -f

                          and the output was:
                          too many FIPS failures, disabling entropy source

                          too many FIPS failures, disabling entropy source

                          No entropy sources working, exiting rngd

                          so .. do i have a CPU Problem ? and how do Math.random() work ? does it depend on hardware rnd ? or a kernel /dev/random ?

                          J 1 Reply Last reply 6 Mar 2020, 15:39
                          0
                          • M MostafaGamal
                            6 Mar 2020, 15:28

                            @JKSH
                            Yes, i have the same processor as in the forum post : AMD Ryzen 7 3700X 8-Core @ 16x 3.6GHz
                            if updating BIOS is the solution ... which BIOS version shall i install ? (My MotherBoard is : Gigabyte B450 Aorus Pro Wifi)

                            also installed rng-tools for testing.
                            that was the output of command

                            rngtest -c 1000 </dev/random
                            

                            rngtest 5
                            Copyright (c) 2004 by Henrique de Moraes Holschuh
                            This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

                            rngtest: starting FIPS tests...
                            rngtest: bits received from input: 20000032
                            rngtest: FIPS 140-2 successes: 999
                            rngtest: FIPS 140-2 failures: 1
                            rngtest: FIPS 140-2(2001-10-10) Monobit: 0
                            rngtest: FIPS 140-2(2001-10-10) Poker: 0
                            rngtest: FIPS 140-2(2001-10-10) Runs: 1
                            rngtest: FIPS 140-2(2001-10-10) Long run: 0
                            rngtest: FIPS 140-2(2001-10-10) Continuous run: 0
                            rngtest: input channel speed: (min=263.549; avg=252860.427; max=6493506.494)bits/s
                            rngtest: FIPS tests speed: (min=45.960; avg=84.689; max=87.094)Mibits/s
                            rngtest: Program run time: 79320378 microseconds

                            also tried this command : sudo rngd -f

                            and the output was:
                            too many FIPS failures, disabling entropy source

                            too many FIPS failures, disabling entropy source

                            No entropy sources working, exiting rngd

                            so .. do i have a CPU Problem ? and how do Math.random() work ? does it depend on hardware rnd ? or a kernel /dev/random ?

                            J Offline
                            J Offline
                            JKSH
                            Moderators
                            wrote on 6 Mar 2020, 15:39 last edited by
                            #13

                            @MostafaGamal said in Problem with Math.random():

                            Yes, i have the same processor as in the forum post : AMD Ryzen 7 3700X 8-Core @ 16x 3.6GHz
                            if updating BIOS is the solution ... which BIOS version shall i install ? (My MotherBoard is : Gigabyte B450 Aorus Pro Wifi)

                            Get the latest version from your motherboard's support website.

                            so .. do i have a CPU Problem ?

                            Probably, since it's not providing entropy.

                            and how do Math.random() work ? does it depend on hardware rnd ? or a kernel /dev/random ?

                            I don't know the internal details, but good modern RNGs should always depend on hardware.

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            M 1 Reply Last reply 6 Mar 2020, 16:10
                            2
                            • J J.Hilk
                              6 Mar 2020, 06:33

                              @MostafaGamal said in Problem with Math.random():

                              but for some reasons .. i can't move on the C++ side for now.

                              you should really consider it, because you're reinventing the wheel ;)

                              Qt has a nice and easy to use QUuid class
                              simply wrap it in a basic class and expose it to QML and import it where you need it.

                              M Offline
                              M Offline
                              MostafaGamal
                              wrote on 6 Mar 2020, 15:51 last edited by
                              #14

                              @J-Hilk
                              sure.

                              taking this into consideration.

                              Thanks a lot :))

                              1 Reply Last reply
                              0
                              • J JKSH
                                6 Mar 2020, 15:39

                                @MostafaGamal said in Problem with Math.random():

                                Yes, i have the same processor as in the forum post : AMD Ryzen 7 3700X 8-Core @ 16x 3.6GHz
                                if updating BIOS is the solution ... which BIOS version shall i install ? (My MotherBoard is : Gigabyte B450 Aorus Pro Wifi)

                                Get the latest version from your motherboard's support website.

                                so .. do i have a CPU Problem ?

                                Probably, since it's not providing entropy.

                                and how do Math.random() work ? does it depend on hardware rnd ? or a kernel /dev/random ?

                                I don't know the internal details, but good modern RNGs should always depend on hardware.

                                M Offline
                                M Offline
                                MostafaGamal
                                wrote on 6 Mar 2020, 16:10 last edited by
                                #15

                                @JKSH

                                i've tried to run

                                quint32 rnd = QRandomGenerator::global()->generate();
                                

                                in a seperate new project for testing too and found the following:
                                rnd value is always constant : 1988610280

                                i think It leaves no doubt that it is CPU issue .. right ?

                                F 1 Reply Last reply 6 Mar 2020, 18:13
                                0
                                • M MostafaGamal
                                  6 Mar 2020, 16:10

                                  @JKSH

                                  i've tried to run

                                  quint32 rnd = QRandomGenerator::global()->generate();
                                  

                                  in a seperate new project for testing too and found the following:
                                  rnd value is always constant : 1988610280

                                  i think It leaves no doubt that it is CPU issue .. right ?

                                  F Offline
                                  F Offline
                                  fcarney
                                  wrote on 6 Mar 2020, 18:13 last edited by
                                  #16

                                  @MostafaGamal said in Problem with Math.random():

                                  quint32 rnd = QRandomGenerator::global()->generate();

                                  Did you run it more than once during the same run? Did you specify a seed value? What seed value?

                                  C++ is a perfectly valid school of magic.

                                  M 1 Reply Last reply 6 Mar 2020, 21:43
                                  0
                                  • F fcarney
                                    6 Mar 2020, 18:13

                                    @MostafaGamal said in Problem with Math.random():

                                    quint32 rnd = QRandomGenerator::global()->generate();

                                    Did you run it more than once during the same run? Did you specify a seed value? What seed value?

                                    M Offline
                                    M Offline
                                    MostafaGamal
                                    wrote on 6 Mar 2020, 21:43 last edited by
                                    #17

                                    @fcarney
                                    no i didn't provide a seed value.

                                    but i ran it more than once, Yes about 9 times
                                    and the same value every time.

                                    btw .. when tracing my UUID Function a new number is generated every 58 tick.

                                    for ex: if i did a for loop 58 times, a new value is generated.

                                    F 1 Reply Last reply 6 Mar 2020, 21:47
                                    0
                                    • M MostafaGamal
                                      6 Mar 2020, 21:43

                                      @fcarney
                                      no i didn't provide a seed value.

                                      but i ran it more than once, Yes about 9 times
                                      and the same value every time.

                                      btw .. when tracing my UUID Function a new number is generated every 58 tick.

                                      for ex: if i did a for loop 58 times, a new value is generated.

                                      F Offline
                                      F Offline
                                      fcarney
                                      wrote on 6 Mar 2020, 21:47 last edited by fcarney 3 Jun 2020, 21:51
                                      #18

                                      @MostafaGamal said in Problem with Math.random():

                                      but i ran it more than once, Yes about 9 times

                                      That is not what I asked:

                                      for(int count=0; count<10; ++count){
                                          quint32 rnd = QRandomGenerator::global()->generate();
                                          qInfo() << rnd;
                                      }
                                      

                                      Does this produce the same output for all 10 values?

                                      Edit:
                                      I get completely different values on every run. So yeah, there is something wrong. I am also not providing a seed.

                                      C++ is a perfectly valid school of magic.

                                      M 1 Reply Last reply 6 Mar 2020, 22:02
                                      1
                                      • F fcarney
                                        6 Mar 2020, 21:47

                                        @MostafaGamal said in Problem with Math.random():

                                        but i ran it more than once, Yes about 9 times

                                        That is not what I asked:

                                        for(int count=0; count<10; ++count){
                                            quint32 rnd = QRandomGenerator::global()->generate();
                                            qInfo() << rnd;
                                        }
                                        

                                        Does this produce the same output for all 10 values?

                                        Edit:
                                        I get completely different values on every run. So yeah, there is something wrong. I am also not providing a seed.

                                        M Offline
                                        M Offline
                                        MostafaGamal
                                        wrote on 6 Mar 2020, 22:02 last edited by
                                        #19

                                        @fcarney

                                        Yes,
                                        The same output every run without a seed.

                                        so, yeah something is not correct here. i'll update my BIOS then re-check it again

                                        F 1 Reply Last reply 6 Mar 2020, 22:04
                                        0
                                        • M MostafaGamal
                                          6 Mar 2020, 22:02

                                          @fcarney

                                          Yes,
                                          The same output every run without a seed.

                                          so, yeah something is not correct here. i'll update my BIOS then re-check it again

                                          F Offline
                                          F Offline
                                          fcarney
                                          wrote on 6 Mar 2020, 22:04 last edited by
                                          #20

                                          @MostafaGamal You have found a really interesting and annoying problem. Please keep us posted as to the outcome.

                                          C++ is a perfectly valid school of magic.

                                          1 Reply Last reply
                                          0

                                          1/21

                                          5 Mar 2020, 20:40

                                          • Login

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