Page 1 of 1

### Gaussian (normal distribution) random number generator

Posted: 13 Aug 2019, 18:15
This function generates a random number with a gaussian or normal distribution (i.e. a bell curve). More info here: https://en.wikipedia.org/wiki/Normal_distribution

This function was originally created to answer the question on this post

Code: Select all

``````rand_gaussian(standard_deviation, mean=0, overflow_error=true) ; by berban - https://www.autohotkey.com/boards/viewtopic.php?f=6&t=67066
{
Static table_file := "ztable.txt" ; put whatever path you stored the table at
Static ztable
if (ztable = "") {
; build the table. source: https://en.wikipedia.org/wiki/Standard_normal_table#Cumulative_from_mean_(0_to_Z)
If FileExist(table_file) {
ztable := []
} else
}
Random, r, 0, 099999 ; this is the precision of the list on wikipedia
r := r / 100000 + 1 / 200000
If (r > 0.5)
r -= 0.5, positive := true
lastKey := 0.00
For a,b in ztable
If (b > r)
Return mean + standard_deviation * (positive ? 1 : -1) * (lastKey * (b - r) + a * (r - ztable[lastKey])) / (b - ztable[lastKey]) ; perform linear interpolation between the two closest values
Else
lastKey := a
If overflow_error
MsgBox, 262160, , Overflow! Those are some crazy odds!
else
Return mean + standard_deviation * (positive ? 1 : -1) * 4.10 ; 4.10 is the next value beyond what the table provides
}``````
Example:

Code: Select all

``````; Will generate a random number averaged around 10 with a standard deviation of ±3
MsgBox % rand_gaussian(3, 10)``````
To run it, you need a lookup table for the z-values. Save the table below somewhere on your pc and edit the first line of the function accordingly.

The table below contains the probability distribution table from here: the first column is the number of standard deviations from the mean in either direction, and the second column is the associated cumulative probability of that deviation.

Code: Select all

``````0.00	0.00000
0.01	0.00399
0.02	0.00798
0.03	0.01197
0.04	0.01595
0.05	0.01994
0.06	0.02392
0.07	0.02790
0.08	0.03188
0.09	0.03586
0.10	0.03983
0.11	0.04380
0.12	0.04776
0.13	0.05172
0.14	0.05567
0.15	0.05962
0.16	0.06356
0.17	0.06749
0.18	0.07142
0.19	0.07535
0.20	0.07926
0.21	0.08317
0.22	0.08706
0.23	0.09095
0.24	0.09483
0.25	0.09871
0.26	0.10257
0.27	0.10642
0.28	0.11026
0.29	0.11409
0.30	0.11791
0.31	0.12172
0.32	0.12552
0.33	0.12930
0.34	0.13307
0.35	0.13683
0.36	0.14058
0.37	0.14431
0.38	0.14803
0.39	0.15173
0.40	0.15542
0.41	0.15910
0.42	0.16276
0.43	0.16640
0.44	0.17003
0.45	0.17364
0.46	0.17724
0.47	0.18082
0.48	0.18439
0.49	0.18793
0.50	0.19146
0.51	0.19497
0.52	0.19847
0.53	0.20194
0.54	0.20540
0.55	0.20884
0.56	0.21226
0.57	0.21566
0.58	0.21904
0.59	0.22240
0.60	0.22575
0.61	0.22907
0.62	0.23237
0.63	0.23565
0.64	0.23891
0.65	0.24215
0.66	0.24537
0.67	0.24857
0.68	0.25175
0.69	0.25490
0.70	0.25804
0.71	0.26115
0.72	0.26424
0.73	0.26730
0.74	0.27035
0.75	0.27337
0.76	0.27637
0.77	0.27935
0.78	0.28230
0.79	0.28524
0.80	0.28814
0.81	0.29103
0.82	0.29389
0.83	0.29673
0.84	0.29955
0.85	0.30234
0.86	0.30511
0.87	0.30785
0.88	0.31057
0.89	0.31327
0.90	0.31594
0.91	0.31859
0.92	0.32121
0.93	0.32381
0.94	0.32639
0.95	0.32894
0.96	0.33147
0.97	0.33398
0.98	0.33646
0.99	0.33891
1.00	0.34134
1.01	0.34375
1.02	0.34614
1.03	0.34849
1.04	0.35083
1.05	0.35314
1.06	0.35543
1.07	0.35769
1.08	0.35993
1.09	0.36214
1.10	0.36433
1.11	0.36650
1.12	0.36864
1.13	0.37076
1.14	0.37286
1.15	0.37493
1.16	0.37698
1.17	0.37900
1.18	0.38100
1.19	0.38298
1.20	0.38493
1.21	0.38686
1.22	0.38877
1.23	0.39065
1.24	0.39251
1.25	0.39435
1.26	0.39617
1.27	0.39796
1.28	0.39973
1.29	0.40147
1.30	0.40320
1.31	0.40490
1.32	0.40658
1.33	0.40824
1.34	0.40988
1.35	0.41149
1.36	0.41308
1.37	0.41466
1.38	0.41621
1.39	0.41774
1.40	0.41924
1.41	0.42073
1.42	0.42220
1.43	0.42364
1.44	0.42507
1.45	0.42647
1.46	0.42785
1.47	0.42922
1.48	0.43056
1.49	0.43189
1.50	0.43319
1.51	0.43448
1.52	0.43574
1.53	0.43699
1.54	0.43822
1.55	0.43943
1.56	0.44062
1.57	0.44179
1.58	0.44295
1.59	0.44408
1.60	0.44520
1.61	0.44630
1.62	0.44738
1.63	0.44845
1.64	0.44950
1.65	0.45053
1.66	0.45154
1.67	0.45254
1.68	0.45352
1.69	0.45449
1.70	0.45543
1.71	0.45637
1.72	0.45728
1.73	0.45818
1.74	0.45907
1.75	0.45994
1.76	0.46080
1.77	0.46164
1.78	0.46246
1.79	0.46327
1.80	0.46407
1.81	0.46485
1.82	0.46562
1.83	0.46638
1.84	0.46712
1.85	0.46784
1.86	0.46856
1.87	0.46926
1.88	0.46995
1.89	0.47062
1.90	0.47128
1.91	0.47193
1.92	0.47257
1.93	0.47320
1.94	0.47381
1.95	0.47441
1.96	0.47500
1.97	0.47558
1.98	0.47615
1.99	0.47670
2.00	0.47725
2.01	0.47778
2.02	0.47831
2.03	0.47882
2.04	0.47932
2.05	0.47982
2.06	0.48030
2.07	0.48077
2.08	0.48124
2.09	0.48169
2.10	0.48214
2.11	0.48257
2.12	0.48300
2.13	0.48341
2.14	0.48382
2.15	0.48422
2.16	0.48461
2.17	0.48500
2.18	0.48537
2.19	0.48574
2.20	0.48610
2.21	0.48645
2.22	0.48679
2.23	0.48713
2.24	0.48745
2.25	0.48778
2.26	0.48809
2.27	0.48840
2.28	0.48870
2.29	0.48899
2.30	0.48928
2.31	0.48956
2.32	0.48983
2.33	0.49010
2.34	0.49036
2.35	0.49061
2.36	0.49086
2.37	0.49111
2.38	0.49134
2.39	0.49158
2.40	0.49180
2.41	0.49202
2.42	0.49224
2.43	0.49245
2.44	0.49266
2.45	0.49286
2.46	0.49305
2.47	0.49324
2.48	0.49343
2.49	0.49361
2.50	0.49379
2.51	0.49396
2.52	0.49413
2.53	0.49430
2.54	0.49446
2.55	0.49461
2.56	0.49477
2.57	0.49492
2.58	0.49506
2.59	0.49520
2.60	0.49534
2.61	0.49547
2.62	0.49560
2.63	0.49573
2.64	0.49585
2.65	0.49598
2.66	0.49609
2.67	0.49621
2.68	0.49632
2.69	0.49643
2.70	0.49653
2.71	0.49664
2.72	0.49674
2.73	0.49683
2.74	0.49693
2.75	0.49702
2.76	0.49711
2.77	0.49720
2.78	0.49728
2.79	0.49736
2.80	0.49744
2.81	0.49752
2.82	0.49760
2.83	0.49767
2.84	0.49774
2.85	0.49781
2.86	0.49788
2.87	0.49795
2.88	0.49801
2.89	0.49807
2.90	0.49813
2.91	0.49819
2.92	0.49825
2.93	0.49831
2.94	0.49836
2.95	0.49841
2.96	0.49846
2.97	0.49851
2.98	0.49856
2.99	0.49861
3.00	0.49865
3.01	0.49869
3.02	0.49874
3.03	0.49878
3.04	0.49882
3.05	0.49886
3.06	0.49889
3.07	0.49893
3.08	0.49896
3.09	0.49900
3.10	0.49903
3.11	0.49906
3.12	0.49910
3.13	0.49913
3.14	0.49916
3.15	0.49918
3.16	0.49921
3.17	0.49924
3.18	0.49926
3.19	0.49929
3.20	0.49931
3.21	0.49934
3.22	0.49936
3.23	0.49938
3.24	0.49940
3.25	0.49942
3.26	0.49944
3.27	0.49946
3.28	0.49948
3.29	0.49950
3.30	0.49952
3.31	0.49953
3.32	0.49955
3.33	0.49957
3.34	0.49958
3.35	0.49960
3.36	0.49961
3.37	0.49962
3.38	0.49964
3.39	0.49965
3.40	0.49966
3.41	0.49968
3.42	0.49969
3.43	0.49970
3.44	0.49971
3.45	0.49972
3.46	0.49973
3.47	0.49974
3.48	0.49975
3.49	0.49976
3.50	0.49977
3.51	0.49978
3.52	0.49978
3.53	0.49979
3.54	0.49980
3.55	0.49981
3.56	0.49981
3.57	0.49982
3.58	0.49983
3.59	0.49983
3.60	0.49984
3.61	0.49985
3.62	0.49985
3.63	0.49986
3.64	0.49986
3.65	0.49987
3.66	0.49987
3.67	0.49988
3.68	0.49988
3.69	0.49989
3.70	0.49989
3.71	0.49990
3.72	0.49990
3.73	0.49990
3.74	0.49991
3.75	0.49991
3.76	0.49992
3.77	0.49992
3.78	0.49992
3.79	0.49992
3.80	0.49993
3.81	0.49993
3.82	0.49993
3.83	0.49994
3.84	0.49994
3.85	0.49994
3.86	0.49994
3.87	0.49995
3.88	0.49995
3.89	0.49995
3.90	0.49995
3.91	0.49995
3.92	0.49996
3.93	0.49996
3.94	0.49996
3.95	0.49996
3.96	0.49996
3.97	0.49996
3.98	0.49997
3.99	0.49997
4.00	0.49997
4.01	0.49997
4.02	0.49997
4.03	0.49997
4.04	0.49997
4.05	0.49997
4.06	0.49998
4.07	0.49998
4.08	0.49998
4.09	0.49998``````