A strong PRNG

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

Re: A strong PRNG

Post by deltarho[1859] »

@neil

I think that you will find that adeyblue posted more than a 'tip'.

A tip would be to use FreeBASIC to send data to rng_test and not 'C'.
neil
Posts: 586
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@adeyblue
Thanks for your expertise in C\C++ coding. The Mersenne Twister Practrand Test has been updated and posted with your suggested modifications. And so far it works great in Windows. Should I comment out this line when running it on Linux? "setmode( _fileno( stdout ), _O_BINARY );"
Last edited by neil on Mar 28, 2023 4:34, edited 2 times in total.
neil
Posts: 586
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@dodicat
Thanks for info about the GCC compiler. I just posted the updated Mersenne Twister Practrand Test. With adeyblue's help it seems to work great in Windows now. I did not test it past 2GB. It still needs further testing.
neil
Posts: 586
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@dodicat
I Just tested your C++ code on Windows it runs but the Practrand Test fails at 32mb.
It doesn't run on Linux at all I get "Segmentation fault (core dumped)"
neil
Posts: 586
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@srvaldez
I have used the -static option before on Linux. If you move your compiled binary exe file to another PC and it doesn't have the lib's or dll's it won't run. In some cases the -static option is a good idea.
deltarho[1859]
Posts: 4305
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: A strong PRNG

Post by deltarho[1859] »

neil wrote:Should I comment out this line when running it on Linux? "setmode( _fileno( stdout ), _O_BINARY );"
If you wrote the piping code in FreeBASIC you would not have to ask that question according to adeyblue's last post. The FreeBASIC code will work on both Linux and Windows without alteration. Why are you using 'C' – this is a FreeBASIC forum? Many members do not write in 'C', including me. :)

There is no advantage in increasing the string size when piping – the law of diminishing returns kicks in. Going from 1MiB to 4MiB sees PractRand running only 2% faster.
neil
Posts: 586
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@deltarho[1859]
Calm down I appreciate your concern, but my question was for adeyblue.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A strong PRNG

Post by dodicat »

Deltatho
practrand uses stdout, does it use the clipboard?
The reason for the C+ stuff was for a comparison (a minute or two outside the box).
Writing all the characters from 0 to 255 in C++ and pascal, when copied from the console and pasted to the forum produces:

Code: Select all

 ☺☻♥♦♣
♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂Çüéâäà
åçêëèïîìÄÅÉæÆôöòûùÿÖÜø£Ø׃áíóúñѪº¿®¬½¼¡«»░▒▓│┤ÁÂÀ©╣║╗╝¢¥┐└┴┬├─┼ãÃ╚╔╩╦╠═╬¤ðÐÊËÈıÍÎÏ┘┌█▄¦Ì▀ÓßÔÒõÕµþÞÚÛÙýݯ´­±‗¾¶§÷¸°¨·¹³²
■   
Doing the same in fb produces:

Code: Select all

 
i.e. nothing.

try it, copy the clipboard and paste it somewhere.

Code: Select all

for n as long=0 to 255
    print chr(n);
next n
sleep
 
There was a different result using C++ and freebasic with practrand.
I was trying to get the root cause.
I have noted this console difference under the bugs thread, and chr(0) is heavily involved in this issue.
At first I thought my computer settings were at fault, I have been messing about with the console here something awfull.
Sometimes we have to step out to look back in.
I only have pascal and c/c++.
I used to have Haskell, but I can't be bothered installing it (or python) to test the console copying capabilities of those two.
neil
Posts: 586
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@dodicat
My FreeBasic code passed Practrand using the command line Pipe in Linux, but deltarho said in failed in Windows. Also I could not use the FreeBasic built in Pipe in Linux because it does not work. I posted that in a previous post. My console character output looks different than yours. Maybe that is why it failed in Windows. I am using FreeBasic 1.09

Code: Select all

	

� !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~��������������������������������������������������������������������������������������������������������������������������������
deltarho[1859]
Posts: 4305
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: A strong PRNG

Post by deltarho[1859] »

dodicat wrote:does it use the clipboard?
Not that I am aware of.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A strong PRNG

Post by dodicat »

I have tested my algorithm written in c++ and c
I draw the line at pascal just now, I'll rain check that one.
Always fails pretty soon.
Maybe I am using the string/char pointer wrongly.
If I run the freebasic code I can hear many beeps (char(7)), with the c++ or c hardly any, so there is a difference.
Here is my fb code for this

my64.bas
and the pipe for practrand
my64 | RNG_test stdin32 -multithreaded

Code: Select all

'my64.bas

namespace rand64
dim as ulongint a,b,c,d,e
const max=18446744073709551615ull

function rndU  as  ulongint
   e = a - ((b shl 7) or (b shr (57)))
   a = b xor ((c shl 13) or (c shr (51)))
   b = c + ((d shl 37) or (d shr (27)))
   c = d + e
   d = e + a
   return d
end function

sub autoinit() constructor
var n=timer
a=n:b=n:c=n:d=n
    for m as long=n to n+2
        a+=m
    rndU()
next
'print "automatic warm up done"
end sub

sub init(n as long)
    rand64.a=n:rand64.b=n:rand64.c=n:rand64.d=n
    for m as long=n to n+2
        rand64.a+=m
    rand64.rndU()
    next
end sub

#define rndf  rand64.rndU/rand64.max
#define rangeI(f,l) clngint((rand64.rndU() mod (((l)-(f))+(1))) + (f))
#define rangeF(f,l) Rndf * ((l) - (f)) + (f)
end namespace


#cmdline "-gen gcc -O 2"


rand64.init(10000)

'=======================================


Dim Shared S As String * 1048576
Dim As Ulong Ptr SPtr, BasePtr
Dim As Long j

SPtr = Cptr(Ulong Ptr, StrPtr( S ))
BasePtr = SPtr
 
Do
  For j = 1 to 262144
    *SPtr = rand64.rndu
    SPtr += 1
  Next
  print S;
  SPtr = BasePtr
Loop
'my64 | RNG_test stdin32 -multithreaded 


 
This should work on Windows and Linux practrand, with a good long life expectancy.
Thanks to deltarho for the binary output to practrand.
deltarho[1859]
Posts: 4305
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: A strong PRNG

Post by deltarho[1859] »

dodicat wrote:If I run the freebasic code I can hear many beeps (char(7)), with the c++ or c hardly any, so there is a difference.
adeyblue has explained the difference. FreeBASIC is Printing binary c++ or c are Printing text.
deltarho[1859]
Posts: 4305
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: A strong PRNG

Post by deltarho[1859] »

I have mentioned many times that I am not a fan of Fourmilab's ENT – for the most part, it tests distribution uniformity.

My first PRNG law states: "Top quality distribution uniformity is a necessary condition for top quality randomness, but it is not sufficient." A classic example is QuickBasic's generator, which has excellent distribution uniformity but poor randomness.

So, I gave the opening post an ENT test on a 256MiB file and got this:

Code: Select all

Entropy = 7.999999 bits per byte.

Optimum compression would reduce the size
of this 268435456 byte file by 0 percent.

Chi square distribution for 268435456 samples is 233.34, and randomly
would exceed this value 83.09 percent of the times.

Arithmetic mean value of data bytes is 127.4924 (127.5 = random).
Monte Carlo value for Pi is 3.141900884 (error 0.01 percent).
Serial correlation coefficient is -0.000047 (totally uncorrelated = 0.0).
None of the first five metrics indicate any evidence that the data is not random. The last metric, the only one related to randomness, is magnificent, with four zeros after the decimal point.

If there was any indication of non randomness, then there would be no point in dusting down PractRand.

Using
NeilRNG0 | rng95_test stdin32 -tlmin 1KB -tlmax 1TB -multithreaded
RNG_test using PractRand version 0.95

fails at 2MiB.

We do not have a winner. I think John Cook's Code Project article, started in 2008, was written before PractRand was initially published at v0.80. If PractRand was available, I am confident that Cook would have used it.
neil
Posts: 586
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@dodicat
I was writing small prng files to "C:\users\win10\prng.bin" to analyze data with your tally.bas. It was interesting to see how the frequency of symbols changed depending on the seed numbers you choose. I knew different seed numbers could make the frequency favor certain symbols. I just never analyzed it. Melissa O’Neill manipulated prng data to spell John D. Cooks name on a data output. https://www.johndcook.com/blog/2017/08/ ... generator/
neil
Posts: 586
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

Using Tally.bas I started with writing my prng to "prng.bin" with a file size of 256 bytes. I could see how many symbols were repeated and many symbols were missing in the file. I kept doubling the size of the file until all 256 symbols were in the file. The results changed of course depending on the seed number. I think this is good way to test an experimental prng even before testing it with Practrand.
I just tested an experimental prng with a 4K file. I had way to many repeated numbers. I had something like 611 number 12's and 320 number 21,s this told me right away not to waste my time with this prng. Maybe this could be Piped into Tally.bas. Although my FreeBasic stdout Pipe doesn't work with Linux. I am not sure about the stdin Pipe.
Post Reply