A strong PRNG

General FreeBASIC programming questions.
Post Reply
neil
Posts: 556
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

I just tested this stdin Pipe and it works with Linux. I am not sure how to Pipe my prng cmdline "prng | tally" Tally.bas would be using stdin with this code after its been modified to work with any prng.

Code: Select all

#ifdef __FB_UNIX__
Const TEST_COMMAND = "ls *"
#else
Const TEST_COMMAND = "dir *.*"
#endif

Open Pipe TEST_COMMAND For Input As #1

Dim As String ln
Do Until EOF(1)
    Line Input #1, ln
    Print ln
Loop

Close #1
sleep
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A strong PRNG

Post by dodicat »

I am not sure you how you are using tally.
Your pipe looks stdout only in windows.
Have you got your mingw on path?
g++ will error to stdin (red print), but you can direct to stdout.
Windows:

Code: Select all

function pipeoutALL(cmd As String) as string 
    dim as string File = "__testcode__"
    var e =Shell (cmd+" "+" >>"+file+" 2>&1")
    Var f=Freefile,txt="",L=0
    Open file For Binary As #f
    L=Lof(f)
    If L>0 Then 
        txt = String(L,0)
        Get #f, , txt
    End If
    Close #f
    Kill File
    return txt
End function


Function pipeout( s As String) As String
    Var f=Freefile
    Open Pipe s For binary As #f 
    s=""
    put #f,,s
    close #f
    Return s
End Function 


print pipeout("g++")

print pipeoutALL("g++")
sleep
 
neil
Posts: 556
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

I have mingw64 in my path; I will have to test it later. I have the Mersenne Twister running on Windows and it is using the Pipe. I had a bad RNG_test file on Linux. I compiled one from src. Your myrandoms.cpp, works now and just finished testing on Linux; it only took 63 minutes for a TeraByte. This was the fastest test ever. How did you get that C++ code to work that fast? I am running 64.bas, the one that beeps in Windows. and its up to 256GB it's a lot slower. I will post the results later, when 64.bas finishes. Did you ever get either of them working in Windows?
neil
Posts: 556
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

Practrand test results. The Mersenne Twister had some sort of an Evaluation fail at 256GB on Windows. It failed at 512GB on Linux. I tested myrandoms.cpp on Windows and it failed at 32mb. I used Practrand v 0.94 They all passed Practrand on Linux except the Mersenne Twister. my64.bas passed Practrand on Windows. My own jcmprng.c failed Practrand on Windows, but passed Practrand on Linux.

Code: Select all

"myrandoms.cpp"
This was run on Linux using Practrand v 0.94
The test took 63 minutes 

rng=RNG_stdin32, seed=unknown
length= 256 gigabytes (2^38 bytes), time= 938 seconds
  no anomalies in 249 test result(s)

rng=RNG_stdin32, seed=unknown
length= 512 gigabytes (2^39 bytes), time= 1884 seconds
  no anomalies in 257 test result(s)

rng=RNG_stdin32, seed=unknown
length= 1 terabyte (2^40 bytes), time= 3789 seconds
  no anomalies in 265 test result(s)

 "my64.bas"
This was run on Linux. The test took 2hrs 56 minutes
rng=RNG_stdin64, seed=unknown
length= 256 gigabytes (2^38 bytes), time= 2318 seconds
  no anomalies in 324 test result(s)

rng=RNG_stdin64, seed=unknown
length= 512 gigabytes (2^39 bytes), time= 4616 seconds
  no anomalies in 335 test result(s)

rng=RNG_stdin64, seed=unknown
length= 1 terabyte (2^40 bytes), time= 9220 seconds
  no anomalies in 346 test result(s)
 
 "my64.bas"
This was run on Windows. The Test took 6 hours
rng=RNG_stdin64, seed=unknown
length= 256 gigabytes (2^38 bytes), time= 5336 seconds
  no anomalies in 284 test result(s)

rng=RNG_stdin64, seed=unknown
length= 512 gigabytes (2^39 bytes), time= 10816 seconds
  no anomalies in 295 test result(s)

rng=RNG_stdin64, seed=unknown
length= 1 terabyte (2^40 bytes), time= 21578 seconds
  no anomalies in 304 test result(s)
Last edited by neil on Mar 30, 2023 2:33, edited 3 times in total.
neil
Posts: 556
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

Anyone Wheres the FreeBasic version of myrandoms.cpp? Thanks
neil
Posts: 556
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

I just passed the Practrand test to a TeraByte with my own algorithm. I did it by analyzing the distribution of numbers using dodicat's symbol counter. The main thing is picking the right seed numbers you can see the changes with the FreeBasic program Tally.bas. Also Practrand does not like the bitwise "and". I get good results using "SHR", "SHL" and "XOR". Either my numbers are not Random and the authors algorithm doesn't know the difference. I guess it could be a bug, If it is then Practrand is useless. I found out now when you have the right tools it can be done. Here's the link to Tally.bas viewtopic.php?p=291896&hilit=storing+da ... ly#p291896
Last edited by neil on Mar 30, 2023 17:36, edited 1 time in total.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A strong PRNG

Post by dodicat »

The my64.bas is the same algorithm as myrandoms.cpp.
On windows I get
...


rng=RNG_stdin32, seed=unknown
length= 256 gigabytes (2^38 bytes), time= 1556 seconds
no anomalies in 284 test result(s)

rng=RNG_stdin32, seed=unknown
length= 512 gigabytes (2^39 bytes), time= 3189 seconds
no anomalies in 295 test result(s)

rng=RNG_stdin32, seed=unknown
length= 1 terabyte (2^40 bytes), time= 6392 seconds
no anomalies in 304 test result(s)

About 106 minutes.

Win 10 has 82 processes and 87 background processes going on just now here.(from task manager)
The cpu has to run all these + practrand
I think Linux is a much leaner and meaner machine.
I believe deltarho has a minimum type windows option where a lot of stuff is turned off when doing these type of tests.
He also introduced chi squared and ENT tests for randomness.
They say that if you measure something, that something itself is changed.(Heisenberg uncertainty principle) started this idea rolling into non quantum life.
So you cannot really measure true randomness I reckon, if it exists in the first place.
Primitive chi squared run.

Code: Select all

#cmdline "-gen gcc -O 2"
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


type range
    as double _max,_min,sd
    as long _maxi,_mini
end type

function mean(a() as double,R as range) as double
    R=type(-1e10,1e10,0,0,0)
    dim as long lb=lbound(a),ub=ubound(a)
    dim as double acc,m
    for n as long=lb to ub
        acc+=a(n)
        if R._max<a(n) then R._max=a(n):R._maxi=n
        if R._min>a(n) then R._min=a(n):R._mini=n
    next
    m=acc/(ub-lb+1)
    acc=0
    for n as long=lb to ub
        acc+=(a(n)-m)*(a(n)-m)
    next
    R.sd =sqr(acc/(ub-lb))
    return m
end function

function chisquared(a() as double,R as range,byref m as double) as double
     m=mean(a(),R)
    dim as double acc
    for n as long=lbound(a) to ubound(a)
        acc+=(a(n)-m)*(a(n)-m)/m
    next
    return acc
end function


dim as ulong lim=50000000
rand64.init(timer)

var low=0
var high=10

 Dim As Double a(low to high)
 
 For n As ulong=1 To lim
    Dim As long u= rangei(low,high)
    a(u)+=1
Next

print "ok"

dim as range R
dim as double m
var c=chisquared(a(),R,m)
print "Mean ";m;"  Standard dev. ";R.sd;" Min ";R._min;" Max ";R._max;" difference ";R._max-R._min
print "chi squared distance ";c
print "Done"
sleep 
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: A strong PRNG

Post by deltarho[1859] »

dodicat wrote:So you cannot really measure true randomness I reckon, if it exists in the first place.
I don't think we can "really measure true randomness" and it probably does not exist in our world.

In the quantum world, randomness is the 'glue' which holds everything together. A truly random process is deterministic. In our world, the macro world, the natural order is chaos which is non-deterministic.

Unified field theory? How do we do that with two physics which are in polar opposition. In the macro world, we cannot. However, in the quantum world we can by virtue of "If it is not prohibited then it is mandatory." The quantum world is awash with what we would regard as contradictions. These contradictions are part and parcel of Schrödinger's wave mechanics, which when subject to the macro world collapse into a single entity determined by its likelihood.
neil
Posts: 556
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

The only thing I have proven is I can write a simple algorithm and pass Practrand to a TeraByte. My latest algorithm can do it with only 2 lines of code. I have been writing them in FreeBasic. I am trying to find one with minimal code. I will then convert it to C language and submit it to one of the experts online. They can analyze it and give their opinions on it. What is random? Maybe I only broke the authors algorithm?
neil
Posts: 556
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@dodicat
Originally I was thinking of Piping my prng test data to Tally.bas "prng | tally" and analyzing data on the fly. Then I would not have write any data to the hard drive. Another method I was thinking maybe my prng test program could be part of tally.bas. I am trying to keep them separate. Is there another way Tally.bas could receive my prng data? This would be without using a Pipe and not writing data to the hard drive.

Code: Select all

' for now I will try this example with the shell command and writing a file to the hard drive.
' this code is not working only an Example
' this would  write a 1k file to the hard drive
' For i = 1 1o 1024
' x = rnd(rnd * 256)
' abyte = x 'converts ushort to a ubyte
' put #1,,abyte ' this would be binary data
' next

' then i use the shell command
' shell "your path to tally.exe" 'This would save time analyzing files
Last edited by neil on Mar 30, 2023 23:20, edited 1 time in total.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: A strong PRNG

Post by dafhi »

hi neil. i saw your post in the Ryzen thread. i haven't touched practrand since about 2020.
if you like cpu metrics, try my unique metaballs demo
neil
Posts: 556
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@dafhi
Very nice thanks for sharing it. On my PC sometimes I am getting 19.59 FPS. What are you getting?
On Linux with compiler options I am now getting 196.87 FPS AMD-Ryzen 7 CPU.
Last edited by neil on Mar 31, 2023 0:28, edited 1 time in total.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: A strong PRNG

Post by dafhi »

200 : -)

thanks for sharing your metric. in comments i share command line options
neil
Posts: 556
Joined: Mar 17, 2022 23:26

Re: A strong PRNG

Post by neil »

@dafhi
is that 200 or 20? After I let in run for a couple of minutes it froze. I am running Linux. I will test it on Windows.
On Windows 10. I am only getting 2.28 FPS. Very slow.
On Linux with compiler options I am now getting 196.87 FPS AMD-Ryzen 7 CPU.
Last edited by neil on Mar 31, 2023 0:27, edited 3 times in total.
dafhi
Posts: 1641
Joined: Jun 04, 2005 9:51

Re: A strong PRNG

Post by dafhi »

hundred

i hard code demo time limits to be kind to the planet
Post Reply