Problem with Math.random()
-
wrote on 5 Mar 2020, 20:40 last edited by
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.4630094115769047i 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.
-
wrote on 5 Mar 2020, 20:54 last edited by
Set the seed value to something that is different every time. Like the system clock.
-
wrote on 5 Mar 2020, 20:58 last edited by
It was working without any seed before.
though, i tried what you asked too: Math.random(new Date().getTime())
still the same constant value -
wrote on 5 Mar 2020, 21:00 last edited by
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.
-
wrote on 5 Mar 2020, 21:10 last edited by
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 ?
-
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 ?
wrote on 5 Mar 2020, 21:32 last edited by@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 ;-) -
wrote on 5 Mar 2020, 21:43 last edited by
-
wrote on 5 Mar 2020, 22:19 last edited by
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.
-
wrote on 5 Mar 2020, 22:26 last edited by
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 ?
-
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 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)
-
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 ?
@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. -
@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)
wrote on 6 Mar 2020, 15:28 last edited by@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 commandrngtest -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 microsecondsalso tried this command : sudo rngd -f
and the output was:
too many FIPS failures, disabling entropy sourcetoo 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 ?
-
@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 commandrngtest -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 microsecondsalso tried this command : sudo rngd -f
and the output was:
too many FIPS failures, disabling entropy sourcetoo 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 ?
@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.
-
@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.wrote on 6 Mar 2020, 15:51 last edited by@J-Hilk
sure.taking this into consideration.
Thanks a lot :))
-
@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.
wrote on 6 Mar 2020, 16:10 last edited byi'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 : 1988610280i think It leaves no doubt that it is CPU issue .. right ?
-
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 : 1988610280i think It leaves no doubt that it is CPU issue .. right ?
wrote on 6 Mar 2020, 18:13 last edited by@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?
-
@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?
wrote on 6 Mar 2020, 21:43 last edited by@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.
-
@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.
wrote on 6 Mar 2020, 21:47 last edited by fcarney 3 Jun 2020, 21:51@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. -
@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.wrote on 6 Mar 2020, 22:02 last edited byYes,
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
-
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
wrote on 6 Mar 2020, 22:04 last edited by@MostafaGamal You have found a really interesting and annoying problem. Please keep us posted as to the outcome.
1/21