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 Updated to NodeBB v4.3 + New Features

Problem with Math.random()

Scheduled Pinned Locked Moved Solved QML and Qt Quick
21 Posts 5 Posters 3.7k Views 2 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
    MostafaGamal
    wrote on 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
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on 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 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 ?

        JonBJ JKSHJ 2 Replies Last reply
        0
        • M MostafaGamal

          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 ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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 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
            • fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on 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 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.HilkJ 1 Reply Last reply
                0
                • M MostafaGamal

                  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 ?

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by JKSH
                  #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
                  3
                  • M MostafaGamal

                    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.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on 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
                    1
                    • JKSHJ JKSH

                      @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 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 ?

                      JKSHJ 1 Reply Last reply
                      0
                      • M MostafaGamal

                        @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 ?

                        JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on 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
                        2
                        • J.HilkJ J.Hilk

                          @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 last edited by
                          #14

                          @J-Hilk
                          sure.

                          taking this into consideration.

                          Thanks a lot :))

                          1 Reply Last reply
                          0
                          • JKSHJ JKSH

                            @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 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 ?

                            fcarneyF 1 Reply Last reply
                            0
                            • M MostafaGamal

                              @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 ?

                              fcarneyF Offline
                              fcarneyF Offline
                              fcarney
                              wrote on 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
                              0
                              • fcarneyF fcarney

                                @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 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.

                                fcarneyF 1 Reply Last reply
                                0
                                • M MostafaGamal

                                  @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.

                                  fcarneyF Offline
                                  fcarneyF Offline
                                  fcarney
                                  wrote on last edited by fcarney
                                  #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
                                  1
                                  • fcarneyF fcarney

                                    @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 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

                                    fcarneyF 1 Reply Last reply
                                    0
                                    • M MostafaGamal

                                      @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

                                      fcarneyF Offline
                                      fcarneyF Offline
                                      fcarney
                                      wrote on 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
                                      • M Offline
                                        M Offline
                                        MostafaGamal
                                        wrote on last edited by
                                        #21

                                        The problem is fixed after updating my BIOS Successfully which updated AGESA to version 1.0.0.3 ABBA

                                        @fcarney Yes, it is very annoying one!

                                        Thanks Everyone for helping me :))

                                        Special Thanks to @JKSH as he mentioned that the problem is a hardware issue not software one.

                                        1 Reply Last reply
                                        4

                                        • Login

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