Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem with Math.random()



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



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



  • It was working without any seed before.

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



  • 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



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



  • @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 ;-)



  • @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 :))



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



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


  • Moderators

    @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


  • Moderators

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



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


  • Moderators

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



  • @J-Hilk
    sure.

    taking this into consideration.

    Thanks a lot :))



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



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



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



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



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



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



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


Log in to reply