Has anyone ported the PCG PRNG to FreeBASIC

General FreeBASIC programming questions.
srvaldez
Posts: 2422
Joined: Sep 25, 2005 21:54

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby srvaldez » Jun 08, 2017 1:05

deltarho[1859] wrote:@srvaldez

Hi, when you created pcg_variants.bi did you edit out all the 128 bits stuff?

yes, without a 128-bit datatype it made no sense to leave it in.
deltarho[1859]
Posts: 2420
Joined: Jan 02, 2017 0:34
Location: UK

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby deltarho[1859] » Jun 08, 2017 1:32

We can create a 128 bit datatype with 'Type/End Type': The first member, High, being a Ulongint and the second member, Low, being a Ulongint.
srvaldez
Posts: 2422
Joined: Sep 25, 2005 21:54

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby srvaldez » Jun 08, 2017 1:41

yes of course, but without implementing the arithmetic functions what's the point?
if you decide to implement them, I would rather not use inline asm even though it would be easier in asm, but portability is more important.
deltarho[1859]
Posts: 2420
Joined: Jan 02, 2017 0:34
Location: UK

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby deltarho[1859] » Jun 08, 2017 1:54

pcg-random.org are using a C struct and there is no assembler in sight - they have done the arithmetic for us.

Actually, it is no big deal - I have an idea which may prove to be of little worth. There is no way that I can predict whether it will bear fruit or not.
srvaldez
Posts: 2422
Joined: Sep 25, 2005 21:54

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby srvaldez » Jun 08, 2017 2:37

it's easy to build fbfrog, get fbfrog from https://github.com/dkl/fbfrog, extract the archive
from command prompt cd into the fbfrog-master folder, then
fbc -w all -gen gcc -Wc -O2 src/*.bas -m fbfrog
after you have built fbfrog you are ready to translate
fbfrog pcg-c-0.94/include/pcg_variants.h
also, often you get a helpful translation when passing a C source file to fbfrog.
deltarho[1859]
Posts: 2420
Joined: Jan 02, 2017 0:34
Location: UK

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby deltarho[1859] » Jun 08, 2017 2:55

Thanks for that. With my background I find github one of the most unfriendliest places on the planet.

It would be easier for me to go through the pcg_variants.h file and take out the appropriate function and then work out what it needs from there; along the lines that counting_pine did with pcg32_random_r.
srvaldez
Posts: 2422
Joined: Sep 25, 2005 21:54

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby srvaldez » Jun 08, 2017 3:15

what's so hard about downloading a zip archive? just click on the green clone or download button.
deltarho[1859]
Posts: 2420
Joined: Jan 02, 2017 0:34
Location: UK

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby deltarho[1859] » Jun 08, 2017 10:12

I finally got there. At one point I got "FreeBASIC\bin\win32\ld.exe: cannot open output file fbfrog.exe: Permission denied". I'm on Windows 10 and sometimes it just not do what is asked of it. A Restart and a repeat asking and it works.

My programs 'Run with Swtches' and 'Filepath to ClipBoard" got a good work out.<smile>

Anyway, thanks srvaldez.

@dkl

Thanks for fbfrog.
MrSwiss
Posts: 3507
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby MrSwiss » Jun 08, 2017 10:34

"FreeBASIC\bin\win32\ld.exe: cannot open output file fbfrog.exe: Permission denied"
That simply means: there is already a instance of FBFrog running (after a: compile & run, action) ...
Close it, before attempting a new compile action.
Typical: if you compile -s gui, but the code is console only. (you'll see nothing, on screen!)
In above case, you'll have to use Task Manager, to "shoot it down".
deltarho[1859]
Posts: 2420
Joined: Jan 02, 2017 0:34
Location: UK

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby deltarho[1859] » Jun 08, 2017 11:26

Thanks MrSwiss.
In above case, you'll have to use Task Manager, to "shoot it down".

I use WinFBE and have to "shoot it down" very often because the last compile I did was for a gui. I would dearly love a command that I can put in the header of a bas such that it overrides what I chose in in WinFBE's Compile menu. Is there one?
MrSwiss
Posts: 3507
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby MrSwiss » Jun 08, 2017 12:00

deltarho[1859] wrote:... that it overrides what I chose in in WinFBE's Compile menu. Is there one?
Yes, there is, but not really simple. See: compiler options: @ ...
deltarho[1859]
Posts: 2420
Joined: Jan 02, 2017 0:34
Location: UK

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby deltarho[1859] » Jun 08, 2017 12:41

What I want is:

Code: Select all

#Compile "gui" or #Compile "console"
TeeEmCee
Posts: 296
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby TeeEmCee » Jun 08, 2017 13:45

I've heard good things about PCG PRNGs. This thread encouraged me to find out more; I read part of the paper and agree that it's great. I've used LCG in a few programs for speed and simplicity, but will definitely use PCG instead from now on, for everything.

The "minimal C version" which is prominent on the website and which counting_pine translated to FB is called "PCG XSH RR 64/32 (LCG)" in the paper (discussed in section 6.3.1).

The C code has been written for optimising C compilers, which can compile the rotation to a single x86 instruction, but FB can't optimisations like that. If you actually cared about speed, you would just link the C code to your FB program, but if you refuse to do that and still care about speed, you're better off using the PCG XSH RS 64/32 variant instead, which "makes a slightly different trade-off—slightly worse statistical performance for slightly greater speed". But it does a shift instead of a rotation, so will more than just slightly faster in FB.

counting_pine wrote:The periodicity of the random numbers is the same as that of the simple PRNG, but the secure hashing means that without a massive lookup table you can't find out the internal state based on its output.
(In the example code's case, it returns 32 bits of output for a 64-bit state anyway, so it's definitely a one-way hash.)

I don't really understand the hashing method, although it's clearly pretty simple.


(Thanks for the great summary.)
The hashing is not "secure". It's not obvious how to reverse it, and that's major goal of the algorithm. But it's vastly simpler than all cryptographically secure random generators (CSRNGs) so I give it zero chance of standing that test -- doesn't matter how non-obvious it is; it only takes one person to come up with an algorithm. It seems that PCG XSH RR 64/32 requires a bare minimum of about 4 outputs to calculate the internal state (given unlimited computation), but it's likely an attack is more practical given a bit more than that. (This PRNG is not billed as secure, so breaking it might not even be considered publishable enough for most cryptographers to work on.)

There is one major shortcoming of the paper, which is that it doesn't compare to any modern CSRNG! The modern generation of sponge-based CSRNGs are very fast and versatile. The website compares to one: ChaCha20, but comparing to just one is far from adequate.

The hashing method is briefly described in section 6.3.1. Recall that in a LCG, higher bits are higher quality, because the b'th bit (counting from LSB) has period 2^b. "The strategy is to perform an xorshift to improve the high bits, then randomly rotate them so that all bits are full period". The top 5, best, bits are used for the rotation amount, then take the next 32 bits, xor in the top 18 bits of the state to give a better mix, before the crucial rotation. The bottom 27 bits are ignored, because they are low quality (but which I would guess weakens the security).
jj2007
Posts: 1462
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby jj2007 » Sep 04, 2017 22:43

deltarho[1859] wrote:Ms O'Neill would do well to include PractRand in the tests. The author of PractRand considered 120 PRNGs and his suite found bias in 78. TestU01(Big Crush) found bias in 50. Dieharder found bias in 25. Dieharder has had its day.


Do you have a PractRand binary (exe or dll) that you could post here? So far I have used Fourmilab's ENT, but PractRand looks interesting (compiling PractRand is not an option for me). With ENT, pcg looks good but not exceptionally good.
srvaldez
Posts: 2422
Joined: Sep 25, 2005 21:54

Re: Has anyone ported the PCG PRNG to FreeBASIC

Postby srvaldez » Sep 04, 2017 23:02

hello jj2007
you can get windows binaries from https://sourceforge.net/projects/pracrand/files/ choose version PractRand_0.92.zip

Return to “General”

Who is online

Users browsing this forum: No registered users and 5 guests