Active Forums => QB64 Discussion => Topic started by: Joshua on December 29, 2018, 06:56:33 pm
Title: How to generate normal / Gaussian randoms
Post by: Joshua on December 29, 2018, 06:56:33 pm
Given RND, does anyone have function that can give a fairly decent approximation to normally distributed values? It doesn't have to be exact, speed of computation is more important.
Title: Re: How to generate normal / Gaussian randoms
Post by: Joshua on December 30, 2018, 08:36:11 pm
For anyone interested, a very approximate normally distributed randoms can be generated by multiplying two random numbers:
NRND = (RND*2-1)*(RND*2-1)
This will give values between -1 and 1, and if you plot them in a histogram, they will be mostly clustered around 0.
Not great, but it's fast, good for introducing a little natural variability to simulations.
Title: Re: How to generate normal / Gaussian randoms
Post by: MWheatley on December 31, 2018, 08:21:02 am
Twice in a week! Something I can make a contribution on.
The code below both calculates normally-distributed random numbers, and plots them in a histogram. I have similar code for a couple of other distributions (Poisson, etc) as well as the original academic citations, somewhere.
I also have code for the t-distribution, for assessing statistical significance.
Title: Re: How to generate normal / Gaussian randoms
Post by: Joshua on December 31, 2018, 02:27:16 pm
Looks like it was written in GWbasic! Would you be able to put the random number generator code portion and put it in a function?
Title: Re: How to generate normal / Gaussian randoms
Post by: bplus on December 31, 2018, 03:35:24 pm
Interesting subject, I myself have wanted "normal" random distributions about a point when graphing.
Here is a function that lets you pick the level of scatter about a point. Level 1 is an even distribution on a range around X, 2 is close to the above codes, a parabolic distribution and levels beyond 2 are powers graphs...
Title: Re: How to generate normal / Gaussian randoms
Post by: Joshua on December 31, 2018, 04:27:45 pm
I'm not sure it's behaving correctly for me. When I hit enter, the category 10 has a big spike that goes off screen. Or how should I read the graph? Maybe I'm doing it wrong.
Title: Re: How to generate normal / Gaussian randoms
Post by: bplus on December 31, 2018, 04:41:05 pm
Read it sideways, the function is finding random numbers around 10 with a plus minus (pm) range of 5.
The data is rounded to integer intervals so 5 is first showing hits and 15 is last showing hits.
It is a frequency distribution most frequent around 10 when level is above 1.
As level increases, the more and more the points are closer to 10.
There is a calculation to see the percent of hits for middle 3 intervals. At level = 1, it is an even distribution so 30% hits, as level increases this percent does spike past 80% heading off screen around 10.
Title: Re: How to generate normal / Gaussian randoms
Post by: bplus on December 31, 2018, 04:54:37 pm
Here is graphic demo using function for drawing points randomly on line across screen, level 1 is even distribution, level 2 gathers points around the center x = 300 with range +-300...
Looks like it was written in GWbasic! Would you be able to put the random number generator code portion and put it in a function?
It will run in QB45 and QB64 just fine. I'm not sure I'm up to putting it into a function (I very rarely use them, preferring subroutines), but the relevant code is the lines beginning with 500.