Engine Works

Under the hood of Alteryx: tips, tricks and how-tos.
ned_blog
8 - Asteroid

I got asked the question of how to generate random numbers with a normal distribution. A quick search online provides all kinds of formulas, it is a simple matter of picking one to implement. I could of course implement this in the formula library in C++, but that doesn't provide instant gratification, so off writing a macro I go. Some of the functions are iterative, which would be slower and more difficult in Alteryx, so I chose a simpler formula. The formulas online also produce a normal distribution with and average of 0 and a standard deviation of 1. Depending on the data you are trying to model, you probably want to change that. Rather than make the use do it, I added some parameters in the macro.

 

Finally, anytime you write a macro it is nice to test it. I wrote a quick module to generate 1,000,000 random #'s and test their average, StdDev, and also produce a chart so you can visually confirm that it is a norm distribution.

 

The results of that are:

Number of Iterations Tested: 1,000,000

Average(should be close to 100): 100.02

Standard Deviation (should be close to 15): 14.98

 

 

My favorite part of this is I used the chart that the test module produced as the icon for the macro. Kind of recursive. Anyway, now generating random numbers with a Gaussian Distribution is as easy as dragging out a macro. The macro and test module can be found here.

 

Comments
Atabarezz
13 - Pulsar

I needed a similar thing to do montecarlo with normal distribution.

Created a CDF and run simulations for different values... It's easy to change replications etc...

 

Wouldn't that be be nice to but a NORMDIST function in math functions though...

 

Picture1.png

 

Best

 

Altan Atabarut @Atabarezz

 

benjamesdavis
6 - Meteoroid

Nice work. Do you know how you could edit the formula (copied below) to introduce a skew to the distribution?

sqrt(-2 * log(1-rand())) * cos(rand() * 25 * PI())*StdDev+AVG

MarketingDS
5 - Atom

Awesome job on the macro! The simulation sampling tool also has a number of distributions including normal/Gaussian. I'm not sure when this tool was added but while exploring I found this thread so I thought I would comment in case other distributions are needed.🙂

Joker_Hazard
11 - Bolide

Link is not working :q

NeilR
Alteryx Alumni (Retired)

@Joker_Hazard the link worked for me - I've attached it to the article (here).

Joker_Hazard
11 - Bolide

Thank you @NeilR !!