Quantum random floats in your applications

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

Quantum random floats in your applications

Postby deltarho[1859] » Jan 08, 2021 8:35

I popped over to the website of the Department of Physics of Humboldt University and downloaded some quantum random numbers. The quantum random number generator (QRNG) is based on the quantum randomness of photon arrival times.

I then mapped each 32-bit value into [0,1) at double precision to give a 32-bit granularity. I settled on 16MB of floats giving a file size of 128MB and called it, imaginatively, 16MBQNums.dat. Image

I don't accept anything so did a Fourmilab's Ent test and a PractRand test on the 64MB binary file. I did a bit test with ENT and got a chi-square of 60.58%. PractRand gave a clean sweep.

All I had to do now was to write some code to use 16MBQNums.dat, which takes about 80 milliseconds to load on my machine, and put into an array.

Four procedures are implemented: Sub _Randomize which uses either a cryptographically random entry point into the array or a fixed value; Function Nums which returns a float; GetSnapshot which notes the current array index value and SetSnapshot to load the index got with GetSnapshot.

A Constructor is used which invokes _Randomize and GetSnapshot, so we initialize with a random 'seeding' and a note of the 'seed' used.

Here is a bi file:

QuantumNums.bi

Code: Select all

'#Console On
#define _16MB 16777216
 
Declare Function MyRandomInt Lib "Advapi32.dll" Alias "SystemFunction036" _
 ( RandomBuffer As Any Ptr, RandomBufferLength As ULong ) As Byte
 
Dim Shared _Rnd(1 To _16MB) As Double
Open "16MBQnums.dat" For Binary As #1
  Get #1, , _Rnd()
Close #1
 
Type Q
  Declare Constructor
  Declare Sub _Randomize( Byval counter As Ulong = 0 )
  Declare Function Nums() As Double
  Declare Sub GetSnapshot()
  Declare Sub SetSnapshot()
  ctr As Ulong
  ctrSnapshot As ULong
End Type
 
Private Sub Q._Randomize( Byval counter As Ulong = 0 )
  Dim As Ulong dummy
  Dim As Any Ptr ptrDummy
  If counter > _16MB Then counter = counter Mod _16MB ' Wrap around
  If counter = 0 Then
    Do
      ptrDummy = Varptr( Dummy )
      MyRandomInt( ptrDummy, 3 ) ' 3 * 8 => 24-bit ==> 16MB
    Loop Until Dummy <> 0
    This.ctr = Dummy
  Else
    This.ctr = counter
  End if
End Sub
 
Private Function Q.Nums() As Double
  This.ctr += 1
  If This.ctr > _16MB Then This.ctr = This.ctr Mod _16MB
  Return _Rnd( This.ctr )
End Function
 
Private sub Q.GetSnapshot()
  This.ctrSnapshot = This.ctr
End Sub
 
Private Sub Q.SetSnapshot()
  This.ctr = This.ctrSnapshot
End Sub
 
Constructor Q
  This._Randomize
  This.GetSnapshot
End Constructor
 
Dim As Q QRnd
#undef Rnd
#defne Rnd QRnd.Nums

At the bottom of the bi file is:

Code: Select all

Dim As Q QRnd
#undef Rnd
#define Rnd QRnd.Nums

so we can use Rnd in our main code. If you Copy & Paste code from the forum, and it uses Rnd just include QuantumNums.bi, put 16MBQNums.dat in the same folder, or reference it, and you are good to go.

Here is an example usage: The average float, 0.5000065768629032, is very good given only 16MB of them.
The effective throughput on my machine is about 523MHz. I did not time the raw speed but Q.Nums which has a wrap around check.

Test.bas

Code: Select all

#include "QuantumNums.bi"
 
Dim As Ulong i
For i = 1 To 4
  Print i;" ";QRnd.Nums
Next
For i = 5 To _16MB - 4
  QRnd.Nums
Next
Print
For i = _16MB-3 To _16MB + 4
  If i = _16MB + 1 Then Print : Print "Wrap around"
  Print i;" ";QRnd.Nums
Next
Print
 
For i = 1 to 1000 ' Burn some numbers
  QRnd.Nums
Next
QRnd.GetSnapshot
For i = 1 to 6
  Print QRnd.Nums
Next
QRnd.SetSnapShot
Print "Repeat"
For i = 1 to 6
  Print QRnd.Nums
Next
Print
 
Dim As Double tot
For i = 1 To _16MB
  tot += QRnd.Nums
Next
Print "Average float:";tot/_16MB
 
Dim As Double t, x
t = Timer
For i = 1 to _16MB
  x = QRnd.Nums
Next
t = Timer - t
Print "Effective throughput";Int(_16MB/(t*10^6));"MHz"
 
Sleep

Typical output:

Code: Select all

1  0.762950548203662
2  0.7928071254864335
3  0.06614233367145062
4  0.4410213676746935
 
16777213  0.7405655682086945
16777214  0.8088323173578829
16777215  0.33781035291031
16777216  0.4878995297476649
 
Wrap around
16777217  0.762950548203662
16777218  0.7928071254864335
16777219  0.06614233367145062
16777220  0.4410213676746935
 
 0.2629275182262063
 0.3861238085664809
 0.8937501716427505
 0.9156534601934254
 0.833033949136734
 0.2078827200457454
Repeat
 0.2629275182262063
 0.3861238085664809
 0.8937501716427505
 0.9156534601934254
 0.833033949136734
 0.2078827200457454
 
Average float: 0.5000065768629032

So, if your application uses less than 16MB of floats, and you want to give it a treat of quantum random floats and don't mind loading a 128MB file then give the above a try.

Before I forget here is the file ( 81.1MB zip ): 16MBQNums.
SARG
Posts: 1230
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Quantum random floats in your applications

Postby SARG » Jan 08, 2021 9:06

@deltarho[1859]
Completly offtopic. Each time you put an image/smiley in your post I only see a generic icon and nearby the text 'Image'.
Maybe others are seeing the same.
The image seems correctly defined with the img tags. And when I directly use the link in a web browser (Chrome) no problem. So I wonder what's happening.
Your image :
Image
Just a test with a png :
Image
Last edited by SARG on Jan 08, 2021 13:13, edited 1 time in total.
deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Quantum random floats in your applications

Postby deltarho[1859] » Jan 08, 2021 9:40

@SARG

I have only ever seen that once in all the time that I have been using my emojis. I waited a while and refreshed and up it popped. So, there was a bottleneck somewhere. So, what are we looking at? How fast is it served? How fast is it rendered.

The two emojis in your post appeared immediately.
And when I directly use the link in a web browser (Chrome) no problem.

Is there an issue with the forum software?

I am still using http as opposed to https. Would that make a difference?

This is not my area of expertise so, really, I don't have a clue.
Maybe others are seeing the same.

Shout up if you do. Nobody has mentioned it before.
Xusinboy Bekchanov
Posts: 357
Joined: Jul 26, 2018 18:28

Re: Quantum random floats in your applications

Postby Xusinboy Bekchanov » Jan 08, 2021 10:53

deltarho[1859] wrote:
Maybe others are seeing the same.

Shout up if you do. Nobody has mentioned it before.

Google Chrome won't show it, but Microsoft Edge will show it.
deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Quantum random floats in your applications

Postby deltarho[1859] » Jan 08, 2021 11:25

Xusinboy Bekchanov wrote:Google Chrome won't show it, but Microsoft Edge will show it.

Google are anti http.

How do we get a directory at users.freebasic-portal.de/ or is that only for the privileged few?
jj2007
Posts: 1944
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Quantum random floats in your applications

Postby jj2007 » Jan 08, 2021 11:43

I see the emojis in Firefox.

users.freebasic-portal.de/ doesn't show anything here. Seems dead.
deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Quantum random floats in your applications

Postby deltarho[1859] » Jan 08, 2021 12:05

I'm on Firefox.
jj2007 wrote:users.freebasic-portal.de/ doesn't show anything here. Seems dead.

You need to use something like these:

Code: Select all

https://users.freebasic-portal.de/sarg/
https://users.freebasic-portal.de/stw/

I wondered what I need to do to use this:

Code: Select all

https://users.freebasic-portal.de/deltarho/

I have a feeling that it is not available to the Hoi polloi which I am a fully paid up member of. Image
SARG
Posts: 1230
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Quantum random floats in your applications

Postby SARG » Jan 08, 2021 13:23

Chrome --> not displayed, Changed to https not displayed
Edge --> displayed

For using (upload) users.freebasic-portal.de you need an account. But not necessary as you have your own server :-)

And it's still well alive, all my work you can download on the forum is uploaded in this site. The daily build (St_W's work) is also there.
deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Quantum random floats in your applications

Postby deltarho[1859] » Jan 08, 2021 15:42

SARG wrote:But not necessary as you have your own server :-)

I do but my website is not https. I could pay for a certificate each month, but I cannot cover that with sales because my software is freeware. I pay the web hosting out of my pocket. OK, I could put a small price on the software, but it is cryptographic, and I made a promise years ago that I would never charge for security software, and I am sticking to that promise. It was not security-based I would charge as much as I could get away with. Quaint, aren't I?

If I could upload to the portal that would solve my emoji issue with Google Chrome.
you need an account

How do I go about getting one?
SARG
Posts: 1230
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Quantum random floats in your applications

Postby SARG » Jan 08, 2021 16:41

deltarho[1859] wrote:How do I go about getting one?

My last contact 10 years ago was Sebastien Steiner ssteiner[at]ssteiner.com. I don't know if still active but you can try.
Maybe someone else could also reply to your request.
jj2007
Posts: 1944
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Quantum random floats in your applications

Postby jj2007 » Jan 08, 2021 16:51

deltarho[1859] wrote:
you need an account
How do I go about getting one?
The deltarho.eu is available!
Get it now! €5.90/1 Year

Plus VAT. They are not clear about it, but I pay 7.31€ per year, and that includes 1GB of server space and an email account.

Attention, I just found out that my TopHost is an Italian company. The one I found above googling for TopHost offers the same tariff of 5.90€ per year, but their domain is top.host, not tophost.it - and they got an awful rating here! Looks like cheating...

https://it.trustpilot.com/review/www.tophost.it gives an "excellent" rating to "my" host, but its site is in Italian, sorry ;-)
deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Quantum random floats in your applications

Postby deltarho[1859] » Jan 08, 2021 17:21

@jj2007

I think that you misunderstood me. Why would I want deltarho.eu? We left the EU a few days ago. Image

I have just changed my domain nameservers from Fasthosts to Cloudfare. Hopefully I will get https for free.

If I have screwed up then I will be in deep 'poo'. Image
deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Quantum random floats in your applications

Postby deltarho[1859] » Jan 08, 2021 20:09

Drum roll!

I expected it to take longer than three hours, but my website is now https.

So, my emojis should now be appearing in Google Chrome.

I must confess that I was sweating a bit - messing about with internet stuff is well outside my comfort zone.
jj2007
Posts: 1944
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Quantum random floats in your applications

Postby jj2007 » Jan 08, 2021 20:45

deltarho[1859] wrote:Why would I want deltarho.eu? We left the EU a few days ago. Image
Just pulling your leg ;-)
deltarho[1859]
Posts: 2846
Joined: Jan 02, 2017 0:34
Location: UK

Re: Quantum random floats in your applications

Postby deltarho[1859] » Jan 08, 2021 21:36

Image

Return to “General”

Who is online

Users browsing this forum: No registered users and 18 guests