A strong PRNG

General FreeBASIC programming questions.
Post Reply
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: A strong PRNG

Post by deltarho[1859] »

The algorithm only started working when I used mod 256.
Well, that is easily explained: The 32-bit output is not random, but mod 256 is up to a point.

Both the author of PractRand and I saw Mersenne Twister fail at 256GiB with PractRand v0.94

There is a way to get Mersenne Twister to go to one TiB with PractRand. One of the authors of Mersenne Twister, I forget which, is still at the same university he was at when the Mersenne Twister was published. I tried contacting him via email. The email did not bounce, but I got no response.
neil
Posts: 591
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

deltarho[1859]
Do you think the mod operator is making the bytes random enough to pass Practrand? or is there a flaw in Practrand?
I have no other way to prove that the modified PRNG is a strong one. I am only going by the Practrand test.
Last edited by neil on Mar 26, 2023 3:31, edited 1 time in total.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: A strong PRNG

Post by deltarho[1859] »

@neil

Your opening post says PRNG by George Marsaglia and is a multiply-with-carry. All wrong. Who said that it was a strong PRNG?

Your opening post has a catastrophic failure at 16MiB and your 8-byte approach has a catastrophic failure at 512MiB.

Your latest post says; "I have no other way to prove my PRNG is a strong one." So, who's PRNG is it? What happened to "This passed all of the diehard battery of tests."

What is the period of the PRNG?

mod 256 tells us nothing about the PRNG.

I cannot find it on the internet.

I need to know the source of the PRNG. Without that, I am out of this thread. :)
neil
Posts: 591
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

JCMPRNG only works with Linux due to a console character translation issue with Windows
I just finished testing my modified C version of the JCMPRNG algorithm. I added one extra line of code and it passed Practrand perfectly to one TeraByte without any warnings. The Practrand test took 3hrs and 56 minutes: Here's the link to the C# source code that I ported to C language. https://www.codeproject.com/Articles/25 ... Generation

Code: Select all

// JCMPRNG To compile using the GCC compiler gcc -Wall jcmprng.c -o jcmprng 
// To test ./jcmprng | ./RNG_test stdin8 -multithreaded
// This is a modified version co the John Cook  / George Margaglia algorithm from codeproject.
#include <stdio.h>
unsigned long r1,r2;
unsigned char rb;

int main() {
r1 = 521288629; r2 = 3624316069;

while(1) {
    r1 = r1 * (r1 ^ r2) + (r1 >> 16);
    r2 = r1 * (r2 ^ r1) + (r2 >> 16);
    r1 = (r1 << 16) + r2;
    r2 = (r2 >> 16) + r1;  // I added this extra line
    rb = (r1 % 256);
    printf("%c",rb);
}
return 0;
}
Last edited by neil on Mar 29, 2023 23:13, edited 5 times in total.
neil
Posts: 591
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

deltarho[1859]
I was having problems with FreeBasic version. The C version is unsigned long r1,r2; The FreeBasic version has to be Dim as UInteger r1,r2 or you will get errors. There must be differences in the programming languages. I made some changes to the original George Marsaglia algorithm. I will just refer to it as the PRNG algorithm. The FreeBasic one just runs to slow. I tested the original version with the diehard battery of tests and it did pass. Here's the link. https://www.codeproject.com/Articles/25 ... Generation
neil
Posts: 591
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

Could some test my 8 Byte test PRNG to at least 1 or 2 GB with Practrand I was told it fails at 512mb. If there are issues on some PC's I would like to know. I would really appreciate the help. I just tested it and here's my results to 2 GB. I used the Command Line Pipe "prng | RNG_test stdin64"

rng=RNG_stdin64, seed=unknown
length= 512 megabytes (2^29 bytes), time= 67.0 seconds
no anomalies in 226 test result(s)

rng=RNG_stdin64, seed=unknown
length= 1 gigabyte (2^30 bytes), time= 132 seconds
no anomalies in 243 test result(s)

rng=RNG_stdin64, seed=unknown
length= 2 gigabytes (2^31 bytes), time= 259 seconds
no anomalies in 261 test result(s)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A strong PRNG

Post by dodicat »

I followed your commands.

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>gcc -Wall neil.c -o prng

C:\Users\Computer\Desktop\fb\test\practrand\msvc12_64bit>prng | rng_test stdin64
RNG_test using PractRand version 0.94
RNG = RNG_stdin64, seed = unknown
test set = core, folding = standard (64 bit)

rng=RNG_stdin64, seed=unknown
length= 16 megabytes (2^24 bytes), time= 2.2 seconds
  Test Name                         Raw       Processed     Evaluation
  BCFN(2+0,13-5,T)                  R= +3005  p =  1e-1176    FAIL !!!!!!!!
  BCFN(2+1,13-5,T)                  R= +2561  p =  1e-1002    FAIL !!!!!!!!
  BCFN(2+2,13-5,T)                  R= +2379  p =  2.7e-931   FAIL !!!!!!!
  BCFN(2+3,13-5,T)                  R= +2334  p =  9.8e-914   FAIL !!!!!!!
  BCFN(2+4,13-6,T)                  R= +2747  p =  3.8e-941   FAIL !!!!!!!
  BCFN(2+5,13-6,T)                  R= +2617  p =  9.6e-897   FAIL !!!!!!!
  BCFN(2+6,13-7,T)                  R= +3102  p =  1.4e-933   FAIL !!!!!!!
  BCFN(2+7,13-8,T)                  R= +3683  p =  1.2e-935   FAIL !!!!!!!
  BCFN(2+8,13-8,T)                  R= +3617  p =  6.9e-919   FAIL !!!!!!!
  BCFN(2+9,13-9,T)                  R= +3614  p =  3.5e-813   FAIL !!!!!!!
  BCFN(2+10,13-9,T)                 R= +3196  p =  3.5e-719   FAIL !!!!!!!
  DC6-9x1Bytes-1                    R= +2610  p =  1e-1510    FAIL !!!!!!!!
  Gap-16:A                          R=+685.3  p =  1.2e-539   FAIL !!!!!!!
  Gap-16:B                          R= +4223  p =  5e-3817    FAIL !!!!!!!!
  FPF-14+6/16:(0,14-1)              R=+16621  p = 0           FAIL !!!!!!!!
  FPF-14+6/16:(1,14-2)              R= +1390  p =  7e-1216    FAIL !!!!!!!!
  FPF-14+6/16:(2,14-2)              R= +1868  p =  2e-1633    FAIL !!!!!!!!
  FPF-14+6/16:(3,14-3)              R= +1753  p =  3e-1536    FAIL !!!!!!!!
  FPF-14+6/16:(4,14-4)              R= +1004  p =  1.7e-820   FAIL !!!!!!!
  FPF-14+6/16:(5,14-5)              R=+724.0  p =  4.5e-600   FAIL !!!!!!!
  FPF-14+6/16:(6,14-5)              R=+366.6  p =  8.5e-304   FAIL !!!!!!
  FPF-14+6/16:(7,14-6)              R=+250.6  p =  7.7e-192   FAIL !!!!!!
  FPF-14+6/16:(8,14-7)              R=+165.6  p =  1.2e-131   FAIL !!!!!
  FPF-14+6/16:(9,14-8)              R= +20.5  p =  8.9e-15    FAIL
  FPF-14+6/16:all                   R=+12854  p = 0           FAIL !!!!!!!!
  FPF-14+6/16:cross                 R= +1827  p =  3e-1434    FAIL !!!!!!!!
  mod3n(5):(0,9-3)                  R= +25.9  p =  1.2e-13    FAIL
  [Low16/64]BCFN(2+0,13-6,T)        R=+876.0  p =  1.9e-300   FAIL !!!!!!
  [Low16/64]BCFN(2+1,13-6,T)        R=+820.9  p =  1.4e-281   FAIL !!!!!!
  [Low16/64]BCFN(2+2,13-6,T)        R=+761.4  p =  3.5e-261   FAIL !!!!!!
  [Low16/64]BCFN(2+3,13-6,T)        R=+773.5  p =  2.4e-265   FAIL !!!!!!
  [Low16/64]BCFN(2+4,13-7,T)        R=+875.6  p =  5.1e-264   FAIL !!!!!!
  [Low16/64]BCFN(2+5,13-8,T)        R= +1044  p =  7.1e-266   FAIL !!!!!!
  [Low16/64]BCFN(2+6,13-8,T)        R= +1024  p =  8.9e-261   FAIL !!!!!!
  [Low16/64]BCFN(2+7,13-9,T)        R= +1130  p =  4.0e-255   FAIL !!!!!!
  [Low16/64]BCFN(2+8,13-9,T)        R= +1082  p =  2.9e-244   FAIL !!!!!!
  [Low16/64]DC6-9x1Bytes-1          R=+327.7  p =  1.6e-184   FAIL !!!!!!
  [Low16/64]Gap-16:A                R=+242.7  p =  6.9e-195   FAIL !!!!!!
  [Low16/64]Gap-16:B                R= +1420  p =  1e-1071    FAIL !!!!!!!!
  [Low16/64]FPF-14+6/16:(0,14-2)    R= +5560  p =  1e-4862    FAIL !!!!!!!!
  [Low16/64]FPF-14+6/16:(1,14-3)    R=+452.0  p =  5.4e-396   FAIL !!!!!!!
  [Low16/64]FPF-14+6/16:(2,14-4)    R=+893.8  p =  3.0e-730   FAIL !!!!!!!
  [Low16/64]FPF-14+6/16:(3,14-5)    R=+862.8  p =  3.8e-715   FAIL !!!!!!!
  [Low16/64]FPF-14+6/16:(4,14-5)    R=+353.2  p =  1.0e-292   FAIL !!!!!!
  [Low16/64]FPF-14+6/16:(5,14-6)    R=+249.3  p =  7.5e-191   FAIL !!!!!!
  [Low16/64]FPF-14+6/16:(6,14-7)    R=+171.5  p =  2.6e-136   FAIL !!!!!
  [Low16/64]FPF-14+6/16:(7,14-8)    R=+127.2  p =  1.4e-91    FAIL !!!!!
  [Low16/64]FPF-14+6/16:(8,14-8)    R= +56.5  p =  1.0e-40    FAIL !!!
  [Low16/64]FPF-14+6/16:all         R= +4722  p =  1e-4250    FAIL !!!!!!!!
  [Low16/64]FPF-14+6/16:cross       R=+502.8  p =  2.1e-432   FAIL !!!!!!!
  [Low4/64]BCFN(2+0,13-7,T)         R=+527.8  p =  2.2e-159   FAIL !!!!!
  [Low4/64]BCFN(2+1,13-7,T)         R=+584.2  p =  2.3e-176   FAIL !!!!!!
  [Low4/64]BCFN(2+2,13-8,T)         R=+695.2  p =  2.1e-177   FAIL !!!!!!
  [Low4/64]BCFN(2+3,13-8,T)         R=+722.7  p =  2.3e-184   FAIL !!!!!!
  [Low4/64]BCFN(2+4,13-8,T)         R=+676.1  p =  1.6e-172   FAIL !!!!!!
  [Low4/64]BCFN(2+5,13-9,T)         R=+783.1  p =  4.4e-177   FAIL !!!!!!
  [Low4/64]BCFN(2+6,13-9,T)         R=+768.1  p =  1.0e-173   FAIL !!!!!!
  [Low4/64]DC6-9x1Bytes-1           R=+161.4  p =  5.9e-93    FAIL !!!!!
  [Low4/64]Gap-16:A                 R=  +5.5  p =  3.0e-4   unusual
  [Low4/64]Gap-16:B                 R= +23.7  p =  3.7e-18    FAIL !
  [Low4/64]FPF-14+6/16:(0,14-4)     R=+341.1  p =  1.3e-278   FAIL !!!!!!
  [Low4/64]FPF-14+6/16:(1,14-5)     R=+140.1  p =  5.7e-116   FAIL !!!!!
  [Low4/64]FPF-14+6/16:(2,14-5)     R= +68.9  p =  6.0e-57    FAIL !!!!
  [Low4/64]FPF-14+6/16:(3,14-6)     R= +43.7  p =  1.6e-33    FAIL !!!
  [Low4/64]FPF-14+6/16:(4,14-7)     R= +45.1  p =  9.3e-36    FAIL !!!
  [Low4/64]FPF-14+6/16:(5,14-8)     R=  +9.5  p =  7.3e-7   unusual
  [Low4/64]FPF-14+6/16:all          R=+343.2  p =  2.4e-290   FAIL !!!!!!
  [Low4/64]FPF-14+6/16:cross        R=+105.9  p =  1.4e-81    FAIL !!!!
  [Low1/64]BCFN(2+2,13-9,T)         R= +22.6  p =  3.4e-6   suspicious
  [Low1/64]BCFN(2+3,13-9,T)         R= +18.7  p =  2.5e-5   mildly suspicious
  [Low1/64]BCFN(2+4,13-9,T)         R= +27.3  p =  2.9e-7   suspicious
  ...and 76 test result(s) without anomalies

 
Using gcc 12 .1 64 bits compiler.
neil
Posts: 591
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@dodicat Thanks I see your using Windows. I am a Linux user. I do have a Windows 10 PC it doesn't boot after an upgrade. I will reinstall Windows 10 tomorrow. Maybe you give me some tips on how to install the GCC compiler on Windows, Then I could test it on Windows also. Does the 8 Byte string output FreeBasic program work past 512mb?
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: A strong PRNG

Post by deltarho[1859] »

@neil

Thanks for the Code Project link.

It is not the best article that I have read by John Cook.

Anyway, good luck with it.

@dodicat
Using gcc 12 .1 64 bits compiler.
I cannot believe I read that. I have fbc 1.10.0 / gcc 12.2, but I would not use that toolchain on the forum.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A strong PRNG

Post by dodicat »

deltarho
It is not a toolchain for my freebasic, it is a c/c++ compiler which I use for writing and compiling c/c++ code.
My fb is completely official 1.09.0 winlibs gcc 9.3.0 with the occasional visit to fb 1.10 for testing.
But thanks for your concern.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A strong PRNG

Post by dodicat »

neil wrote: Mar 26, 2023 9:07 @dodicat Thanks I see your using Windows. I am a Linux user. I do have a Windows 10 PC it doesn't boot after an upgrade. I will reinstall Windows 10 tomorrow. Maybe you give me some tips on how to install the GCC compiler on Windows, Then I could test it on Windows also. Does the 8 Byte string output FreeBasic program work past 512mb?
download from
https://winlibs.com/
Expand and put the bin folder on your system path.
No need for any fancy install.

stdin8 fails at 16 Mb.
Provoni
Posts: 514
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: A strong PRNG

Post by Provoni »

neil wrote: Mar 21, 2023 3:41 A simple algorithm for a strong PRNG. This PRNG math formula is George Marsaglia's multiply with carry. George Marsaglia was a mathematician and computer scientist. This passed all of the diehard battery of tests. The PRNG part is just 3 lines of code.

Code: Select all

'' Pseudo random number generator by Neil
'' The prng math formula is George Marsaglia's multiply with carry
'' This passed all of the diehard battery of tests
'' This is a strong prng

dim as uinteger i,r1,r2
dim as ushort sr

'' 2 seed numbers from timer different numbers everytime
r2 = timer: r1 = r2 * 2

'' 2 seed numbers for same numbers every time on first run
'' r1 = 521288629:r2 = 3624316069

For i = 1 to 20

'' The magic is done with the next 3 lines of code
   r1 = 36969 * (r1 and 65535) + (r1 shr 16)
   r2 = 18000 * (r2 and 65535) + (r2 shr 16)
   r1 = (r1 shl 16) + r2  ''r1 = large prng numbers

'' sr = small prng numbers 0 - 255 
   sr = (r1 mod 256)
   print sr,r1

next

print
print i-1;" Random Numbers"
sleep
Thanks,

A few questions.

Are there any requirements for the 2 seed numbers?

You used mod 256, what is the safe limit here?

Do you know what the period of the generator is?
neil
Posts: 591
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@Provoni
I posted the link to the source code of the algorithm and it has other info about the algorithm on a previous post I left for deltarho[1859]. I hope this helps.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: A strong PRNG

Post by dafhi »

i feed ubyte rng to my mini suite then switch to ulong for practrand

Code: Select all

type myStateType as ubyte

dim as mystatetype a,b
dim as mystatetype c
neil
Posts: 591
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

I was running RNG_test v0.95 on Linux and my prng's tested perfect. I am trying to test my prng C code on a Windows 10 Pc. I am using the GCC compiler 12,2 and the Practrand source code wont compile on Windows. I can't find any binary RNG_test.exe files for v0.95 Does anyone know where to find them? So far I all find is source code. Thanks
Post Reply