A strong PRNG

General FreeBASIC programming questions.
Post Reply
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: A strong PRNG

Post by srvaldez »

I don't know anything about PracRand but apart for some warnings it compiled ok, you can get my build of PracRand
neil
Posts: 555
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@srvaldez
Thanks it runs ok. I am still having trouble even with Practrand ver 0.95. My prng algorithms pass the RNG_test on Linux but fail on Windows. I am not sure what the problem is. Did you compile Practrand on the command line with the GCC compiler?
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: A strong PRNG

Post by srvaldez »

neil wrote: Mar 27, 2023 1:45 Did you compile Practrand on the command line with the GCC compiler?
yes
perhaps deltarho[1859] or dodicat can give you help
neil
Posts: 555
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

Mersenne Twister Practrand Test This is now working in Windows thanks to adeyblue's expertise in C\C++ coding. This needs further testing to see if it fails. It is suppose to fail at 512GB. according to M.E. O'Neill and John D. Cook.https://www.pcg-random.org/posts/how-to ... trand.html https://www.johndcook.com/blog/2017/08/ ... practrand/

Code: Select all

// To Compile on Windows C:\users\Win10\mingw64\bin\g++ -Wall mtwist.cpp -o mtwist
// To Compile on Linux g++ -Wall mtwist.cpp -o mtwist 
// Then ./mtwist | ./RNG_test stdin32 -multithreaded
// Practrand test should fail at 512GB

#include <cstdio>
#include <cstdint>
#include <random>
#include <io.h>
#include <fcntl.h>

int main()
{
   _setmode( _fileno( stdout ),  _O_BINARY );
        std::mt19937 rng(42);
    while (1) {
        uint32_t value = rng();
        fwrite((void*) &value, sizeof(value), 1, stdout);
    }
}
Last edited by neil on Mar 28, 2023 3:59, edited 5 times in total.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A strong PRNG

Post by dodicat »

Hi neil
My results

Code: Select all

PS C:\Users\Computer\Desktop\fb\test\practrand\msvc12_64bit> cmd
Microsoft Windows [Version 10.0.19044.2604]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Computer\Desktop\fb\test\practrand\msvc12_64bit>g++ -Wall mtwist.cpp -o mtwist

C:\Users\Computer\Desktop\fb\test\practrand\msvc12_64bit>mtwist | RNG_test stdin32
RNG_test using PractRand version 0.94
RNG = RNG_stdin32, seed = unknown
test set = core, folding = standard (32 bit)

rng=RNG_stdin32, seed=unknown
length= 64 megabytes (2^26 bytes), time= 2.0 seconds
  Test Name                         Raw       Processed     Evaluation
  BCFN(2+0,13-3,T)                  R=+146.8  p =  2.9e-69    FAIL !!!!
  BCFN(2+1,13-3,T)                  R=+121.1  p =  3.9e-57    FAIL !!!!
  BCFN(2+2,13-4,T)                  R=+126.6  p =  2.8e-55    FAIL !!!!
  BCFN(2+3,13-4,T)                  R=+143.7  p =  1.0e-62    FAIL !!!!
  BCFN(2+4,13-5,T)                  R=+159.1  p =  1.8e-62    FAIL !!!!
  BCFN(2+5,13-5,T)                  R=+156.7  p =  1.6e-61    FAIL !!!!
  BCFN(2+6,13-6,T)                  R=+192.7  p =  1.9e-66    FAIL !!!!
  BCFN(2+7,13-6,T)                  R=+180.9  p =  2.1e-62    FAIL !!!!
  BCFN(2+8,13-7,T)                  R=+220.5  p =  5.7e-67    FAIL !!!!
  BCFN(2+9,13-8,T)                  R=+297.7  p =  1.6e-76    FAIL !!!!
  BCFN(2+10,13-8,T)                 R=+274.3  p =  1.4e-70    FAIL !!!!
  BCFN(2+11,13-9,T)                 R=+288.0  p =  7.9e-66    FAIL !!!!
  BCFN(2+12,13-9,T)                 R=+290.9  p =  1.7e-66    FAIL !!!!
  DC6-9x1Bytes-1                    R=+147.9  p =  1.1e-93    FAIL !!!!!
  Gap-16:A                          R=+150.9  p =  1.9e-131   FAIL !!!!!
  Gap-16:B                          R=+838.7  p =  1.0e-746   FAIL !!!!!!!
  FPF-14+6/16:(0,14-0)              R= +8452  p =  3e-7821    FAIL !!!!!!!!
  FPF-14+6/16:(1,14-0)              R=+707.1  p =  5.2e-654   FAIL !!!!!!!
  FPF-14+6/16:(2,14-1)              R=+497.6  p =  9.0e-441   FAIL !!!!!!!
  FPF-14+6/16:(3,14-2)              R=+356.1  p =  3.0e-311   FAIL !!!!!!
  FPF-14+6/16:(4,14-2)              R=+214.5  p =  2.4e-187   FAIL !!!!!!
  FPF-14+6/16:(5,14-3)              R=+148.6  p =  4.7e-130   FAIL !!!!!
  FPF-14+6/16:(6,14-4)              R=+107.8  p =  4.8e-88    FAIL !!!!
  FPF-14+6/16:(7,14-5)              R= +75.5  p =  1.8e-62    FAIL !!!!
  FPF-14+6/16:(8,14-5)              R= +47.4  p =  3.9e-39    FAIL !!!
  FPF-14+6/16:(9,14-6)              R= +65.4  p =  4.3e-50    FAIL !!!!
  FPF-14+6/16:all                   R= +5467  p =  2e-5130    FAIL !!!!!!!!
  FPF-14+6/16:cross                 R= +87.7  p =  1.8e-77    FAIL !!!!
  mod3n(5):(0,9-2)                  R= +23.6  p =  5.4e-13    FAIL
  [Low8/32]BCFN(2+0,13-5,T)         R= +53.6  p =  3.5e-21    FAIL !!
  [Low8/32]BCFN(2+1,13-5,T)         R= +50.5  p =  5.9e-20    FAIL !!
  [Low8/32]BCFN(2+2,13-5,T)         R= +52.0  p =  1.4e-20    FAIL !!
  [Low8/32]BCFN(2+3,13-5,T)         R= +49.4  p =  1.6e-19    FAIL !
  [Low8/32]BCFN(2+4,13-6,T)         R= +47.1  p =  1.4e-16    FAIL !
  [Low8/32]BCFN(2+5,13-6,T)         R= +35.4  p =  1.5e-12    FAIL
  [Low8/32]BCFN(2+6,13-7,T)         R= +69.4  p =  1.6e-21    FAIL !!
  [Low8/32]BCFN(2+7,13-8,T)         R= +73.0  p =  1.7e-19    FAIL !
  [Low8/32]BCFN(2+8,13-8,T)         R= +66.5  p =  7.9e-18    FAIL !
  [Low8/32]BCFN(2+9,13-9,T)         R= +90.8  p =  1.6e-21    FAIL !!
  [Low8/32]BCFN(2+10,13-9,T)        R= +96.2  p =  9.7e-23    FAIL !!
  [Low8/32]DC6-9x1Bytes-1           R= +19.3  p =  6.9e-11    FAIL
  [Low8/32]FPF-14+6/16:(0,14-1)     R=+314.4  p =  2.3e-278   FAIL !!!!!!
  [Low8/32]FPF-14+6/16:(1,14-2)     R= +32.5  p =  3.2e-28    FAIL !!
  [Low8/32]FPF-14+6/16:(2,14-2)     R= +28.4  p =  1.2e-24    FAIL !!
  [Low8/32]FPF-14+6/16:(3,14-3)     R= +17.8  p =  2.5e-15    FAIL
  [Low8/32]FPF-14+6/16:(4,14-4)     R= +10.9  p =  8.6e-9   very suspicious
  [Low8/32]FPF-14+6/16:(5,14-5)     R=  +7.3  p =  6.0e-6   unusual
  [Low8/32]FPF-14+6/16:(6,14-5)     R= +11.9  p =  1.0e-9   very suspicious
  [Low8/32]FPF-14+6/16:(7,14-6)     R= +10.2  p =  8.2e-8   suspicious
  [Low8/32]FPF-14+6/16:(8,14-7)     R= +12.3  p =  1.3e-9   very suspicious
  [Low8/32]FPF-14+6/16:all          R=+236.7  p =  1.1e-221   FAIL !!!!!!
  [Low8/32]FPF-14+6/16:cross        R= +21.0  p =  5.4e-17    FAIL !
  [Low1/32]BCFN(2+0,13-6,T)         R=+109.8  p =  5.0e-38    FAIL !!!
  [Low1/32]BCFN(2+1,13-6,T)         R= +95.5  p =  3.9e-33    FAIL !!!
  [Low1/32]BCFN(2+2,13-7,T)         R=+100.4  p =  7.7e-31    FAIL !!!
  [Low1/32]BCFN(2+3,13-7,T)         R=+104.5  p =  4.5e-32    FAIL !!!
  [Low1/32]BCFN(2+4,13-8,T)         R=+124.1  p =  1.9e-32    FAIL !!!
  [Low1/32]BCFN(2+5,13-8,T)         R=+126.3  p =  5.2e-33    FAIL !!!
  [Low1/32]BCFN(2+6,13-9,T)         R=+133.5  p =  4.1e-31    FAIL !!!
  [Low1/32]BCFN(2+7,13-9,T)         R=+137.9  p =  4.2e-32    FAIL !!!
  [Low1/32]DC6-9x1Bytes-1           R= +12.0  p =  1.0e-6   very suspicious
  ...and 79 test result(s) without anomalies

 
neil
Posts: 555
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@dodicat
Thanks for the test. Windows is working fine now with Practrand and the C++ Mersenne Twister Test.
Last edited by neil on Mar 29, 2023 22:15, edited 2 times in total.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A strong PRNG

Post by dodicat »

I think you are correct.
This in c++ only gets 256 mb
PS C:\Users\Computer\Desktop\fb\test\practrand\msvc12_64bit> cmd
Microsoft Windows [Version 10.0.19044.2604]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Computer\Desktop\fb\test\practrand\msvc12_64bit>myrandoms | RNG_test stdin32 -multithreaded
RNG_test using PractRand version 0.94
RNG = RNG_stdin32, seed = unknown
test set = core, folding = standard (32 bit)

rng=RNG_stdin32, seed=unknown
length= 256 megabytes (2^28 bytes), time= 2.7 seconds
Test Name Raw Processed Evaluation
BCFN(2+0,13-2,T) R>+99999 p = 0 FAIL !!!!!!!!
BCFN(2+1,13-2,T) R>+99999 p = 0 FAIL !!!!!!!!
bla
bla
c++ code following deltarho's work
myrandoms.cpp

Code: Select all

#include<iostream>
#include <time.h> 
#include <string>


namespace random
{
unsigned long long a,b,c,d,e;
const unsigned long long max=18446744073709551615ull;

unsigned long long& rndu()
{
   e = a - ((b << 7) or (b >> (57)));
   a = b xor ((c << 13) or (c >> (51)));
   b = c + ((d << 37) or (d >> (27)));
   c = d + e;
   d = e + a;
   return d;
}


void reseed(int n)
    {a=n;b=n;c=n;d=n;
    for(int m =n; m< n+10;m++)  rndu()*=m;}
      
struct seed  {seed() {	reseed(1000000); }};
namespace  {seed start;}

 long long range(long long f ,long long l)
    {return (rndu() % ((l-f)+1)) + f;}

 double range(double f ,double l)
 	{return ((double)rndu()/max) * (l - f) + f;}
}// namespace random ends
   
int main()
{
	std::string s;
	s.resize (1048576);
unsigned long * sptr;
unsigned long * baseptr;
long j;
sptr=(unsigned long *) &s[0];
baseptr = sptr;
random::rndu()=time(NULL);
while(1)
{
    for(j=1;j<262145;j++)
    {
        *sptr = (unsigned long) random::rndu();
        sptr += 1;   
    }
 std::cout<<s;
    sptr = baseptr;
}

}

//g++ -Wall myrandoms.cpp -o myrandoms
//myrandoms | RNG_test stdin32 -multithreaded 

 
How is it in Linux?
neil
Posts: 555
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@dodicat
It wouldn't compile

Code: Select all

gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

root@linux:~# g++ -Wall myrandoms.cpp -o myrandoms
myrandoms.cpp:6:11: error: ‘namespace random { }’ redeclared as different kind of entity
    6 | namespace random
      |           ^~~~~~
In file included from /usr/include/c++/9/cstdlib:75,
                 from /usr/include/c++/9/ext/string_conversions.h:41,
                 from /usr/include/c++/9/bits/basic_string.h:6496,
                 from /usr/include/c++/9/string:55,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from myrandoms.cpp:1:
/usr/include/stdlib.h:401:17: note: previous declaration ‘long int random()’
  401 | extern long int random (void) __THROW;
      |                 ^~~~~~
myrandoms.cpp: In function ‘int main()’:
myrandoms.cpp:45:1: error: ‘random’ is not a class, namespace, or enumeration
   45 | random::rndu()=time(NULL);
      | ^~~~~~
myrandoms.cpp:50:33: error: ‘random’ is not a class, namespace, or enumeration
   50 |         *sptr = (unsigned long) random::rndu();
      |                                 ^~~~~~
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A strong PRNG

Post by dodicat »

Maybe the word random is already in use somewhere?
Try changing the name of the namespace maybe.
neil
Posts: 555
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@dodicat
I changed the namespace to "rndom" The namespace fixed the compile problem no errors or warnings. When I run it on Linux it gets "Segmentation fault (core dumped)" My Linux gcc g++ compiler version 9.4.0 I also tested your test code on Windows 10 it compiles fine. When I run it. The code execution cannot proceed because because libgcc_s_seh.dll was not found. libstdc++-6.dll was not found.
Reinstalling the program may fix the problem. My Windows 10 gcc g++ compiler version 12.2.0
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A strong PRNG

Post by dodicat »

Maybe you need to put the mingw64\bin folder on the system path in windows
Mine is in
"C:\mingw64\bin"
I can call g++ or gcc from the console at any location by typing g++ or gcc.
I also have direct access to the other .exe files in the bin folder.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: A strong PRNG

Post by srvaldez »

neil
that is a common problem, you either need to have the dependent dll's in in same location as your executable or perhaps have the dll's on the PATH
but often you can eliminate those dependencies by compiling with -static option, that's how I built pracrand
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: A strong PRNG

Post by deltarho[1859] »

If you have PractRand working OK, there are quite a few command line switches here when using v0.95.

srvaldez very kindly provided us with binaries for v0.95 in his post Mar 27, 2023 00:53.

v0.95 is the first time that I am aware of where the binaries were not supplied by the author. Another oddity is v0.95 is not mentioned in versions.txt. It is as if he was completing v0.95 got interrupted and never went back.

@srvaldez
It is PractRand, incidentally, short for Practically Random.

If a PRNG comes 'off the rails' fairly early, then I fine tune the starting point with '-tlmin 1KB' (sans quotes) and have used that for a few years. So, we get tests on 1KB, 2KB, 4KB, 8KB, 16KB ... and so on. You will need that for testing Linear congruential generators (LCGs) as they tend to fail very early on in a PractRand test. They fail because the lower bits of a 32-bit or 64-bit output are far from random. However, that may not be that important when converting the 32-bit or 64-bit output to a float within [0,1).

Some other switches may prove useful or not, as the case may be. :)
Last edited by deltarho[1859] on Mar 27, 2023 20:45, edited 1 time in total.
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: A strong PRNG

Post by adeyblue »

C & C++ output on Windows defaults to text mode instead of binary mode, so every \n (0xa byte) in the output will output a \r\n (0xd 0xa bytes) instead. On Linux there is no diference between text and binary modes, but Windows has this translation. This extends to everything using stdout/cout like fwrite or cout.write as well as fprintf or cout << etc.

Code: Select all

freopen(NULL, "wb", stdout);  
This doesn't do anything, to fix it you need

Code: Select all

#include <io.h>
#include <fcntl.h>
int main()
{
    _setmode( _fileno( stdout ),  _O_BINARY );
...
There's no C++ specific way, that's how you change it for cout too. Of course

Code: Select all

int i = 1;
cout << i;
will still convert the output to text (character '1'), you need cout.write to output binary values (0x00000001)

FreeBasic on Windows doesn't have this translation so
Print !"\n";
will only output 0xa.
Last edited by adeyblue on Mar 27, 2023 21:17, edited 1 time in total.
neil
Posts: 555
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@deltarho[1859]
I am starting over with these tests. I deleted all v0.94 and v0.95 Practrand files on Linux and Windows. I am only downloading files from https://sourceforge.net/projects/pracrand/files/ and using PractRand_0.94.zip I will you see if this makes a difference. I noticed adeyblue posted a tip. I will look into that also.
Post Reply