Updated: 31 March 2014
Use the table-valued function RANDBINOM to generate a sequence of random integers from the binomial distribution for a given probability of success @p and a given number of trials @Trials.
SELECT * FROM [wctMath].[wct].[RANDBINOM](
the number of rows to generate. @MaxIterations must be of the type int or of a type that implicitly converts to int.
the success probability in each trial. @p must be of the type float or of a type that implicitly converts to float.
the number of trials. @Trials must be of the type int or of a type that implicitly converts to int.
RETURNS TABLE (
[Seq] [int] NULL,
[X] [int] NULL
· @p must be greater than or equal to zero and less than or equal to 1 (0 <= @p <= 1).
· @Trials must be greater than zero.
· If @p is NULL then @p is set to 0.
· If @Trials is NULL then @Trials is set to 1.
· If @Rows is less than 1 then no rows are returned.
In this example we create a sequence 1,000,000 random numbers from a binomial distribution with @p = 0.3 and @Trials = 500, COUNT the results, paste then into Excel and graph them.
COUNT(*) as [COUNT]
This produces the following result.
In this example we generate 1,000,000 random numbers from a binomial distribution with probability of .70 and number of trials = 140. We calculate the mean, standard deviation, skewness, and excess kurtosis from the resultant table and compare those values to the expected values for the distribution.
DECLARE @size as int = 1000000
DECLARE @p as float = 0.70
DECLARE @trials as float = 140
DECLARE @mean as float = @p*@trials
DECLARE @var as float = @mean*(1e+00-@p)
DECLARE @stdev as float = SQRT(@var)
DECLARE @skew as float =(1 - 2*@p)/@stdev
DECLARE @kurt as float =(1-6*@p*(1e+00-@p))/@var
AVG(cast(x as float)) as mean_BINOM,
STDEVP(x) as stdev_BINOM,
wct.SKEWNESS_P(x) as skew_BINOM,
wct.KURTOSIS_P(x) as kurt_BINOM
PIVOT(sum(val_stat) FOR fn_name in([RANDBINOM],[EXPECTED])) P
This produces the following result (your result will be different).