2

I am trying to generate random numbers using std::random_device but each time I run the program they are the same. I tried the same code using an online IDE and it ran as expected. According to an answer here, this is a bug in Cygwin and MSYS2.

How can I upgrade MSYS2? I tried running the commands pacman -Syu and pacman -Su in its terminal but it had errors:

MSYS ~ $ pacman -Syuu error: mingw32: key
"4A6129F4E4B84AE46ED7F635628F528CF3053E04" is unknown error: key
"4A6129F4E4B84AE46ED7F635628F528CF3053E04" could not be looked up
remotely error: mingw64: key
"4A6129F4E4B84AE46ED7F635628F528CF3053E04" is unknown error: key
"4A6129F4E4B84AE46ED7F635628F528CF3053E04" could not be looked up
remotely error: msys: key "4A6129F4E4B84AE46ED7F635628F528CF3053E04"
is unknown error: key "4A6129F4E4B84AE46ED7F635628F528CF3053E04" could
not be looked up remotely :: Synchronizing package databases... 
mingw32                  472.5 KiB   305K/s 00:02
[#####################] 100%  mingw32.sig              438.0   B 
0.00B/s 00:00 [#####################] 100% error: mingw32: key "4A6129F4E4B84AE46ED7F635628F528CF3053E04" is unknown error: key
"4A6129F4E4B84AE46ED7F635628F528CF3053E04" could not be looked up
remotely error: failed to update mingw32 (invalid or corrupted
database (PGP signature))  mingw64                  475.0 KiB   346K/s
00:01 [#####################] 100%  mingw64.sig              438.0   B
0.00B/s 00:00 [#####################] 100% error: mingw64: key "4A6129F4E4B84AE46ED7F635628F528CF3053E04" is unknown error: key
"4A6129F4E4B84AE46ED7F635628F528CF3053E04" could not be looked up
remotely error: failed to update mingw64 (invalid or corrupted
database (PGP signature))  msys                     189.8 KiB   164K/s
00:01 [#####################] 100%  msys.sig                 438.0   B
0.00B/s 00:00 [#####################] 100% error: msys: key "4A6129F4E4B84AE46ED7F635628F528CF3053E04" is unknown error: key
"4A6129F4E4B84AE46ED7F635628F528CF3053E04" could not be looked up
remotely error: failed to update msys (invalid or corrupted database
(PGP signature)) error: failed to synchronize all databases

Is there any alternatives or work around for generating random numbers on Windows?

northerner
  • 756
  • 5
  • 21
  • are you seeding your RNG? – Andy Aug 01 '20 at 05:08
  • @Andy isn't that what random device does? To my understanding the problem is it isn't implemented correctly in msys2 (but I can't believe that no one can generate random numbers on Windows). – northerner Aug 01 '20 at 05:11
  • eh yeah -- no idea how that works. And you can generate random numbers on windows... but they are windows-only APIs (like `CryptGenRandom()`) -- of course Windows has PRNG like (`srand`,`rand`), but who wants to deal with seeds any more. ETA: my mistake CryptGenRandom is PRNG as well... and deprecated! – Andy Aug 01 '20 at 05:14
  • Which version of GCC are you using? The same as in the linked question? You might need to do a fresh reinstall of MSYS and MinGW to get the latest version. Version 4.8 is *very* old by now, and didn't have full C++11 implementation. – Some programmer dude Aug 01 '20 at 05:24
  • @Someprogrammerdude g++ (Rev3, Built by MSYS2 project) 9.1.0 – northerner Aug 01 '20 at 05:28
  • Something's not right with your MSYS2 intsallation. Try reinstalling it. – HolyBlackCat Aug 01 '20 at 07:38
  • @Andy Stephan T. Lavavej (the major implementor of the STL for MSVC at Microsoft) claims in his presentation ["rand() Considered Harmful"](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) that at least in case of the Visual Studio the `std::random_device` is implemented as cryptographically secure. – heap underrun Aug 01 '20 at 08:38
  • I got it to work. I followed a link a on https://github.com/msys2/MSYS2-packages/issues/2058 and found some steps. The order the steps are presented are incomplete/incorrect so I answered my own question. Please feel free to give feedback as I'm not really an expert when it comes to package managers. – northerner Aug 01 '20 at 08:47
  • the orginal link example works fine on Cygwin with g++ (GCC) 9.3.0 – matzeri Aug 01 '20 at 15:58
  • I don't know how gcc implements `std::random_device`, but if you want to seed radomly, you could either us the current timestamp (ideally the microseconds and nanoseconds part, depending on the resolution of your clock), or read from `/dev/urandom`. – user1934428 Feb 25 '22 at 07:03

2 Answers2

2

I got it to work by referring to this news page on MSYS2. I had to run pacman -Sydd pacman before the rest of the steps could work.

northerner
  • 756
  • 5
  • 21
1

To complete @northener's answer, I run the following command:

$ curl -O https://repo.msys2.org/msys/x86_64/msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz
$ curl -O https://repo.msys2.org/msys/x86_64/msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz.sig
$ pacman-key --verify msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz.sig
$ pacman -U msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz

As the news says: "If you can't even import the key and command still fails, you have to convince pacman to not care about those databases for a while, for example like this:"

$ pacman -U --config <(echo) msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz

"If you still see signature errors, resetting your pacman key store might help:"

$ rm -r /etc/pacman.d/gnupg/
$ pacman-key --init
$ pacman-key --populate msys2

Finally command is ok

$ pacman -Syu

Bug report: https://github.com/msys2/MSYS2-packages/issues/2097

GeoGyro
  • 487
  • 12
  • 32