Share My Creation Random Number - Normal Distribution Server

Hi All,

I have made the fast normal distribution random number generator. The server is named randomNoGen.exe and takes one parameter the port number. Like speedySpline if you don't provide a parameter the port defaults to 27015. The example test app is using 27016.

Basically you send a command: 1, the mean and the standard deviation and you get back the values as doubles. The random number generator is seeded with the computer clock so you will get a different set of number for each run. If you wish to use the same number set for different runs of you program then get the numbers once and save that number set to a file.

The raw results of a single run for 20,000 Values. As you can see it takes about 0.35 seconds to do 20,000 points. The second screen shot shows a histogram of that data.

randNorm30000.png


randNorm30000Histogram.png



As you can see the data is pretty close: mean given = 1.0 data mean = 0.999, standard deviation given = 2.5 data SD = 2.515

Sadly I can't provide the source code for the random number algorithm as it is proprietary, however I have attached the B4J test program which include randomNoGen.exe in the objects directory.

Hope you find this useful.

Best regards
Rob
 

Attachments

  • randomNoTest_002.zip
    38 KB · Views: 95

rgarnett1955

Active Member
Licensed User
Hi Erel

I had a look at SecureRandom, but it creates uniform random bytes as far as I can see not normally distributed ones. Even if you grouped up large numbers of bytes you would still need to apply an algorithm to generate a normal distribution. Or maybe I'm not understanding properly.

I will still give it a try.

Best regards
Rob
 

Erel

Administrator
Staff member
Licensed User
Something like this:
B4X:
Sub Process_Globals
    Private sr As SecureRandom
    Private raf As RandomAccessFile
    Private buffer(8) As Byte
End Sub

Sub AppStart (Args() As String)
    raf.Initialize3(buffer, True)
    For i = 1 To 100
        Log(RandomDouble)
    Next
End Sub

Private Sub RandomDouble As Double
    sr.GetRandomBytes(buffer)
    Return raf.ReadDouble(0)
End Sub
 

emexes

Expert
Licensed User
I had a look at SecureRandom, but it creates uniform random bytes as far as I can see not normally distributed ones.
From memory, the SD of 12 uniform-distribution random numbers 0..1 added together is 1.

Sum 12 random unsigned bytes, subtract 1580, divide by 255, and you should get a normal distribution with SD 1 around 0.
 
Last edited:

rgarnett1955

Active Member
Licensed User
Hi Erel,

That's what I thought, but its not working. Viz:

secureRandom_001.png



I'm running version B4J 8.8

Best regards
Rob
 

avalle

Active Member
Licensed User
Hi Rob, interesting work.
A bit off-topic, but I can't find the B4Xtoastmessage library so I get errors. Can you point me to it?

Thanks
Andrea
 

William Lancee

Active Member
Licensed User
From memory, the SD of 12 uniform-distribution random numbers 0..1 added together is 1.
Sum 12 random unsigned bytes, subtract 1580, divide by 255, and you should get a normal distribution with SD 1 around 0.

https://en.wikipedia.org/wiki/Central_limit_theorem

@emexes is right, I always use the following.

B4X:
    Dim markTime As Long = DateTime.now
    Dim sum, sumsq As Double
    Dim rnormal As Float
    For i = 1 To 1000000
        rnormal = 0
        For j = 1 To 12
            rnormal = rnormal + .000006 * Rnd(-83333, 83334)
        Next
        sum = sum + rnormal
        sumsq = sumsq + rnormal * rnormal
    Next
    Dim strMean As String = NumberFormat2(sum / 1000000, 1, 4, 4, False)
    Dim strStdev As String = NumberFormat2(Sqrt((sumsq - sum * sum / 1000000) / 999999), 1, 4, 4, False)
    Log((DateTime.now - markTime) & TAB & strMean & TAB & strStdev)    'on my system in release mode this time hovers around
                                                                        '300 milliseconds for 1000000 random normal points
 
Last edited:

rgarnett1955

Active Member
Licensed User
See my previous answer. I'm talking about Encryption library. Not B4XEncryption.

Encryption library is a B4A + B4J library.

Erel,

Where do I find this library called "Encryption" it doesn't appear in my library window.

I put the word Encryption in the libraries search window and I get:

jB4XEncryption and B4xEncryption, neither of these work

Best regards
Rob

I found it. Andrew Graham had it hidden in an example App.

It would be nice if the Help pages on the various functions included a link to the library zip files.

Finding these things in the forum is like searching for the proverbial needle in the haystack.

I used it and it works fine.

Best regards
Rob
 
Last edited:

avalle

Active Member
Licensed User
It's not internal, you can get it from here:
 

rgarnett1955

Active Member
Licensed User
https://en.wikipedia.org/wiki/Central_limit_theorem

@emexes is right, I always use the following.

B4X:
    Dim markTime As Long = DateTime.now
    Dim sum, sumsq As Double
    Dim rnormal As Float
    For i = 1 To 1000000
        rnormal = 0
        For j = 1 To 12
            rnormal = rnormal + .000006 * Rnd(-83333, 83334)
        Next
        sum = sum + rnormal
        sumsq = sumsq + rnormal * rnormal
    Next
    Dim strMean As String = NumberFormat2(sum / 1000000, 1, 4, 4, False)
    Dim strStdev As String = NumberFormat2(Sqrt((sumsq - sum * sum / 1000000) / 999999), 1, 4, 4, False)
    Log((DateTime.now - markTime) & TAB & strMean & TAB & strStdev)    'on my system in release mode this time hovers around
                                                                        '300 milliseconds for 1000000 random normal points

Hi William,

Thanks for that. I gave it a test drive and it worked perfectly.

Best regards
Rob
 

rgarnett1955

Active Member
Licensed User
Hi Rob, interesting work.
A bit off-topic, but I can't find the B4Xtoastmessage library so I get errors. Can you point me to it?

Thanks
Andrea
Hi Andrea,

Sorry I didn't get back to you earlier.

The library I used is attached as a zip file. I downloaded it from the forum, but I can't remember where so I have attached it. You should probably check that it is the latest revision.

Best regards
Rob
 

Attachments

  • b4xToastMsgLib.zip
    8 KB · Views: 70

rgarnett1955

Active Member
Licensed User
Thanks! I think it's not always easy to search in the forum...

I agree, it's a dogs breakfast. The search facility is hopeless as I can see no way of qualifying the search input such as complete word, whole phrase etc.

I often go to google and do searches from there by prefixing my search with "B4x" including the quotes to force the search to only include b4x stuff.

As you can see from the other replies in this forum thread, I wasted my time creating a random number generator in C++ as you can use the Encryption library and a simple algorithm to calculate a normally distributed number set from uniform random numbers. I suppose I should have asked. I'm sadder, but wiser now!

Best regards
Rob
 
Top