({{, (RefLink[RandomVariate, paclet:ref/RandomVariate])[dist] 
gives a pseudorandom variate from the symbolic distribution dist.}, {, (RefLink[RandomVariate, paclet:ref/RandomVariate])[dist,n]
gives a list of n pseudorandom variates from the symbolic distribution dist.}, {, (RefLink[RandomVariate, paclet:ref/RandomVariate])[dist,{(Subscript[n, 1]),(Subscript[n, 2]),…}]
gives an (Subscript[n, 1])× (Subscript[n, 2])×… array of pseudorandom variates from the symbolic distribution dist.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[RandomVariate, paclet:ref/RandomVariate]) can generate random variates for continuous, discrete, or mixed distributions specified as a symbolic distribution.

(RefLink[RandomVariate, paclet:ref/RandomVariate]) gives a different sequence of pseudorandom numbers whenever you run the Wolfram Language. You can start with a particular seed using (RefLink[SeedRandom, paclet:ref/SeedRandom]).

With the setting (RefLink[WorkingPrecision, paclet:ref/WorkingPrecision])->p, random numbers of precision p will be generated.

OpenersPrimaryExamplesSection(Spacer1)Examples  (78)

OpenersExampleSection0(Spacer1)Basic Examples  (5)

Simulate a continuous probability distribution:

RandomVariate[NormalDistribution[]]

data=RandomVariate[NormalDistribution[1,3],10^4];

Show[
Histogram[data,20,"ProbabilityDensity"],
Plot[PDF[NormalDistribution[1,3],x],{x,-9,9},PlotStyle->Thick]]

Simulate a discrete probability distribution:

RandomVariate[PoissonDistribution[3],10]

data=RandomVariate[PoissonDistribution[10],10^4];

Show[
Histogram[data,Automatic,"ProbabilityDensity"],
DiscretePlot[PDF[PoissonDistribution[10],x],{x,0,20},PlotStyle->PointSize[Medium]]]

Simulate a multivariate continuous distribution:

RandomVariate[BinormalDistribution[1/2],5]

data=RandomVariate[BinormalDistribution[1/2],10^4];

{Histogram3D[data,20,"ProbabilityDensity"],
Plot3D[PDF[BinormalDistribution[1/2],{x,y}]//Evaluate,{x,-3,3},{y,-3,3}]}

Simulate a multivariate discrete distribution:

RandomVariate[MultivariatePoissonDistribution[1,{2,3}],5]

data=RandomVariate[MultivariatePoissonDistribution[1,{2,3}],10^4];

{Histogram3D[data,20,"Probability",PlotRange->{{0,10},{0,10},All}],
DiscretePlot3D[PDF[MultivariatePoissonDistribution[1,{2,3}],{x,y}],{x,0,10},{y,0,10},PlotRange->All,ExtentSize->Full]}

Generate random numbers from a mixture distribution:

dist=MixtureDistribution[{1,2},{NormalDistribution[1,1/2],NormalDistribution[4,5/3]}];

data=RandomVariate[dist,10^5];

Show[
Histogram[data,45,"ProbabilityDensity"],
Plot[PDF[dist,x],{x,-2,10},PlotStyle->PointSize[Medium]]]

OpenersExampleSection1(Spacer1)Scope  (22)

OpenersExampleSubsection0(Spacer1)Basic Uses  (5)

Use (RefLink[RandomVariate, paclet:ref/RandomVariate]) to generate arrays of different sizes and dimensions:

dist=NormalDistribution[3,5];

A vector:

RandomVariate[dist,{2}]

Dimensions[%]

A matrix:

RandomVariate[dist,{2,3}]

Dimensions[%]

A rank-3 tensor:

RandomVariate[dist,{2,3,4}]

Dimensions[%]

Generate high-precision random variates:

RandomVariate[NormalDistribution[],WorkingPrecision->20]

RandomVariate[NormalDistribution[],WorkingPrecision->100]

Use (RefLink[SeedRandom, paclet:ref/SeedRandom]) to get repeatable random values:

SeedRandom[0];RandomVariate[NormalDistribution[],3]

SeedRandom[0];RandomVariate[NormalDistribution[],3]

Generate random variates for a univariate continuous distribution:

RandomVariate[RayleighDistribution[3]]

Discrete univariate distribution:

RandomVariate[GeometricDistribution[1/7],4]

Continuous multivariate distribution:

RandomVariate[MultivariateTDistribution[{{1,1/2},{1/2,1}},5],2]

Discrete multivariate distribution:

RandomVariate[MultivariatePoissonDistribution[1,{3,4,5}],{2,3}]

Generate random variates for a quantity distribution:

RandomVariate[NormalDistribution[(Quantity[180, "cm", centimeters, "Centimeters"]),(Quantity[5, "cm", centimeters, "Centimeters"])]]

Generate array of variates:

RandomVariate[NormalDistribution[(Quantity[180, "cm", centimeters, "Centimeters"]),(Quantity[5, "cm", centimeters, "Centimeters"])],100]

Generate random vectors:

RandomVariate[BinormalDistribution[{(Quantity[17, "mi""/""h", miles per hour, (("Miles")/("Hours"))]),(Quantity[42, "°F", degrees Fahrenheit, "DegreesFahrenheit"])},{(Quantity[0.1, "mi""/""h", miles per hour, (("Miles")/("Hours"))]),(Quantity[2.1, "°F", degrees Fahrenheit, "DegreesFahrenheit"])},0.2],30]

OpenersExampleSubsection1(Spacer1)Parametric Distributions  (4)

Generate random variates for univariate continuous distributions:

RandomVariate[BetaDistribution[2,3]]

RandomVariate[UniformDistribution[{-2,11}],5]

RandomVariate[NormalDistribution[],{2,3}]

Show[Histogram[RandomVariate[ChiSquareDistribution[4],10^5],{0,15,0.5},"PDF"],Plot[PDF[ChiSquareDistribution[4],x],{x,0,15}]]

Generate random variates for univariate discrete distributions:

RandomVariate[BinomialDistribution[12,1/3]]

RandomVariate[DiscreteUniformDistribution[{-2,11}],5]

RandomVariate[PoissonDistribution[3],{2,3}]

Show[
Histogram[RandomVariate[SkellamDistribution[50,30],10^4],{0,40,2},"PDF"],
DiscretePlot[PDF[SkellamDistribution[50,30],x],{x,0,40}]]

Generate random variates for multivariate continuous distributions:

RandomVariate[UniformDistribution[{{-2,11},{3,15}}]]

RandomVariate[MultinormalDistribution[{1,2,3},{{2,1/2,-1/3},{1/2,1,0},{-1/3,0,2/3}}],2]

RandomVariate[DirichletDistribution[{1,3,5,7}],{2,3}]

{Histogram3D[RandomVariate[BinormalDistribution[{1,2},{1.5,2},0.6],10^4],20,"PDF"],Plot3D[PDF[BinormalDistribution[{1,2},{1.5,2},0.6],{x,y}],{x,-5,6},{y,-3,7}]}

Generate random variates for multivariate discrete distributions:

RandomVariate[DiscreteUniformDistribution[{{-2,11},{3,15}}]]

RandomVariate[MultinomialDistribution[12,{1/3,1/2,1/6}],2]

RandomVariate[MultivariateHypergeometricDistribution[10,{2,3,4,7}],{2,3}]

{Histogram3D[RandomVariate[MultivariatePoissonDistribution[1,{2,3}],10^4],40,"PDF"],
DiscretePlot3D[PDF[MultivariatePoissonDistribution[1,{2,3}],{x,y}],{x,0,12},{y,0,12},ExtentSize->0.5]}

OpenersExampleSubsection2(Spacer1)Nonparametric Distributions  (4)

Generate random variates for a univariate (RefLink[EmpiricalDistribution, paclet:ref/EmpiricalDistribution]):

𝒟=EmpiricalDistribution[RandomVariate[ExponentialDistribution[1/2],10^5]];

Show[Histogram[RandomVariate[𝒟,10^3],Automatic,"PDF"],Plot[PDF[ExponentialDistribution[1/2],x],{x,0,10}]]

Using a multivariate empirical distribution:

𝒟=EmpiricalDistribution[RandomVariate[BinormalDistribution[1/7],10^5]];

RandomVariate[𝒟,3]

Using a univariate (RefLink[HistogramDistribution, paclet:ref/HistogramDistribution]):

𝒟=HistogramDistribution[RandomVariate[ExponentialDistribution[1/7],10^5]];

RandomVariate[𝒟,{3,2}]

A multivariate histogram distribution:

𝒟=HistogramDistribution[RandomVariate[BinormalDistribution[.75],10^4]];

EstimatedDistribution[RandomVariate[𝒟,10^3],BinormalDistribution[a]]

Using a univariate (RefLink[KernelMixtureDistribution, paclet:ref/KernelMixtureDistribution]):

𝒟=KernelMixtureDistribution[RandomVariate[NormalDistribution[],1000]];

Show[Histogram[RandomVariate[𝒟,(10^4)],Automatic,"PDF"],
Plot[PDF[𝒟,x],{x,-4,4}]]

Using censored data with (RefLink[SurvivalDistribution, paclet:ref/SurvivalDistribution]):

eventTimes={10,7,23,22,6,25,20,19,6,17,6,13};

censorIndicators={0,0,0,0,0,1,1,1,0,1,0,0};

𝒟=SurvivalDistribution[EventData[eventTimes,censorIndicators]];

RandomVariate[𝒟,15]

OpenersExampleSubsection3(Spacer1)Derived Distributions  (9)

Generate random variates for a (RefLink[TransformedDistribution, paclet:ref/TransformedDistribution]):

SeedRandom[0];RandomVariate[TransformedDistribution[Exp[-5 x],x\[Distributed]ExponentialDistribution[1]],{3,5}]

An equivalent way of generating the same random variates:

SeedRandom[0];
m=RandomReal[ExponentialDistribution[1],{3,5}];Map[Exp[-5 #]&,m]

Generate random variates for a (RefLink[ProductDistribution, paclet:ref/ProductDistribution]):

𝒟=ProductDistribution[ExponentialDistribution[2],ExponentialDistribution[3]];

SeedRandom[0];RandomVariate[𝒟,4]

An equivalent way of generating the same random variates:

SeedRandom[0];Transpose[{RandomVariate[ExponentialDistribution[2],4],RandomVariate[ExponentialDistribution[3],4]}]

Using a component mixture of normal distributions:

𝒟 =MixtureDistribution[{1,3}, {NormalDistribution[0,1],NormalDistribution[6,2]}];

data = RandomVariate[𝒟,10^5];

Show[Histogram[data,50,"PDF"],Plot[PDF[𝒟,x],{x,-20,20}]]

Parameter mixture of exponential distributions:

𝒟=ParameterMixtureDistribution[ExponentialDistribution[λ],λ\[Distributed]UniformDistribution[{1,2}]];

data=RandomVariate[𝒟,10^4];

Compare the variance of the sample with the variance of the distribution:

{Variance[data],Variance[𝒟]}//N

Truncated normal distribution:

𝒟=TruncatedDistribution[{-2,2},NormalDistribution[]];

data=RandomVariate[𝒟,10^5];

Show[Histogram[data,50,"PDF", PlotRange->{{-3,3},All}],Plot[PDF[𝒟,x],{x,-3,3}], Frame->True,Axes->False]

Censored exponential distribution:

𝒟=CensoredDistribution[{0,20},ExponentialDistribution[1/3]];

data=RandomVariate[𝒟,10^5];

Compare the kurtosis for the sample with the kurtosis for the distribution:

{Kurtosis[data],Kurtosis[𝒟]}//N

Marginal distribution:

𝒟=MarginalDistribution[DirichletDistribution[{1,3,4,5}],2];

data=RandomVariate[𝒟,10^5];

Compute probabilities using the sample and the marginal distribution:

Probability[x^2+3x<1,x\[Distributed]data]//N

NProbability[x^2+3x<1,x\[Distributed]𝒟]

Copula distribution:

𝒟 = CopulaDistribution[{"Frank",3}, {UniformDistribution[{0,1}], UniformDistribution[{0,1}]}];

data=RandomVariate[𝒟,10^5];

Compare the value of a moment for the sample with that for the copula distribution:

Moment[data,{2,1}]

Moment[𝒟,{2,1}]

N[%]

Formula distribution:

𝒟=ProbabilityDistribution[(Sqrt[2]/Pi)(1/(1+x^4)),{x,-∞,∞}];

data=RandomVariate[𝒟,10^5];

Show[Histogram[data,200,"PDF", PlotRange->{{-3,3},All}],Plot[PDF[𝒟,x],{x,-3,3}], Frame->True,Axes->False]

OpenersExampleSection2(Spacer1)Options  (1)

OpenersExampleSubsection4(Spacer1)WorkingPrecision  (1)

By default (RefLink[MachinePrecision, paclet:ref/MachinePrecision]) random numbers are generated for continuous distributions:

SeedRandom[1234];
RandomVariate[ExponentialDistribution[2],5]

SeedRandom[1234];
RandomVariate[ExponentialDistribution[2],5,WorkingPrecision->MachinePrecision]

Use the (RefLink[WorkingPrecision, paclet:ref/WorkingPrecision]) option to generate numbers with arbitrary precision:

RandomVariate[ExponentialDistribution[2],5,WorkingPrecision-> 25]

OpenersExampleSection3(Spacer1)Applications  (30)

OpenersExampleSubsection5(Spacer1)Pictures of Random Data  (6)

Generate random data for a continuous distribution and compare its histogram to the PDF:

data=RandomVariate[NormalDistribution[1,3],10^4];

Show[
Histogram[data,20,"PDF"],
Plot[PDF[NormalDistribution[1,3],x],{x,-9,9},PlotStyle->Thick]]

Generate random data for a discrete distribution and compare its histogram to the PDF:

data=RandomVariate[BinomialDistribution[10,0.4],10^4];

Show[
Histogram[data,{-0.5,10.5,1},"PDF"],
DiscretePlot[PDF[BinomialDistribution[10,0.4],x],{x,0,10},PlotStyle->PointSize[Medium]]]

Generate random data for a bivariate distribution and compare its histogram to the PDF:

data=RandomVariate[BinormalDistribution[{1,2},{1.5,2},0.6],10^4];

Show[Histogram3D[data,{{-5,6,1},{-3,7,1}},"PDF",ChartBaseStyle->Opacity[0.5]],
Plot3D[PDF[BinormalDistribution[{1,2},{1.5,2},0.6],{x,y}],{x,-5,6},{y,-3,7},PlotStyle->ColorData[97,1],Mesh->None]]

Compare the plots of random data for continuous multivariate distributions:

Table[Graphics[{Hue[RandomReal[]],PointSize[Small],Point[RandomVariate[dist,10^3]]},AxesOrigin->{0,0}],{dist,{DirichletDistribution[{1,2,3}],MultinormalDistribution[{0,0},{{1,1/2},{1/2,1}}],MultivariateTDistribution[{0,0},{{1,1/2},{1/2,1}},5]}}]

Consider vectors with standard normal components:

Graphics[{Blue,Table[Arrow[{{0,0},RandomVariate[NormalDistribution[],2]}],{100}]}]

The angle will follow a uniform distribution:

vectors=RandomVariate[NormalDistribution[],{(10^4),2}];

Show[Histogram[Apply[ArcTan,vectors,{1}],Automatic,"PDF"],Plot[PDF[UniformDistribution[{-π,π}],x],{x,-π,π},PlotStyle->Thick]]

The norm will follow a Rayleigh distribution:

Show[Histogram[Map[Norm,vectors],25,"PDF"],Plot[PDF[RayleighDistribution[1],x],{x,0,5},PlotStyle->Thick]]

TransformedDistribution[Norm[{u,v}],{u\[Distributed]NormalDistribution[],v\[Distributed]NormalDistribution[]}]

Consider three-dimensional vectors with standard normal components:

Graphics3D[{Blue,Table[Arrow[{{0,0,0},RandomVariate[NormalDistribution[],3]}],{200}]}]

The θ angle in spherical coordinates follows a uniform distribution:

vectors=RandomVariate[NormalDistribution[],{(10^4),3}];

Show[Histogram[Apply[Function[{x,y,z},ArcTan[y/x]],vectors,{1}],Automatic,"PDF"],Plot[PDF[UniformDistribution[{-π/2,π/2}],x],{x,-π,π},PlotStyle->Thick]]

The norm will follow a χ distribution:

Show[Histogram[Map[Norm,vectors],20,"PDF"],Plot[PDF[ChiDistribution[3],x],{x,0,5},PlotStyle->Thick]]

TransformedDistribution[Norm[{x,y,z}],{x,y,z}\[Distributed]ProductDistribution[{NormalDistribution[],3}]]

OpenersExampleSubsection6(Spacer1)Distribution Properties  (5)

Verify the Poisson approximation to the binomial distribution using random variates:

n=2000;p=1/30;

r1=RandomVariate[BinomialDistribution[n,p],10^5];

{Mean[r1],StandardDeviation[r1],Kurtosis[r1]}//N

λ=n p;

r2=RandomVariate[PoissonDistribution[λ],10^5];

{Mean[r2],StandardDeviation[r2],Kurtosis[r2]}//N

Define a truncated triangular distribution:

dist=TruncatedDistribution[{1,4},TriangularDistribution[{0,7}]];

Generate random numbers from this distribution:

data=RandomVariate[dist,10^5];

Show[
Histogram[data,20,"ProbabilityDensity",PlotRange-> All],
Plot[PDF[dist,x],{x,0,5},PlotStyle->Thick,Exclusions->None],AxesOrigin->{0,0}]

Mean, variance, and kurtosis:

{N[Mean[dist]],Mean[data]}

{N[Variance[dist]],Variance[data]}

{N[Kurtosis[dist]],Kurtosis[data]}

Moments:

{N[Moment[dist,5]],Moment[data,5]}

{N[CentralMoment[dist,5]],CentralMoment[data,5]}

Probabilities and expectations:

{NProbability[E^x<3,x\[Distributed]dist],Probability[E^x<3,x\[Distributed]data]//N}

{NExpectation[E^x,x\[Distributed]dist],Expectation[E^x,x\[Distributed]data]//N}

Simulate the distribution of a function of a random variable:

dist=TransformedDistribution[x^2,x\[Distributed]ExponentialDistribution[1]];

r=RandomVariate[dist,10^6];

Compare the mean and variance of the sample with those for the distribution:

{Mean[r],Variance[r]}

{Mean[dist],Variance[dist]}

Generate random data from a normal distribution:

𝒟=NormalDistribution[2,3];
data=RandomVariate[𝒟,10^4];

Define nonparametric distributions using this data:

Histogram𝒟=HistogramDistribution[data];

KernelMixture𝒟=KernelMixtureDistribution[data];

SmoothKernel𝒟=SmoothKernelDistribution[data];

Empirical𝒟=EmpiricalDistribution[data];

Compare the mean and variance of these distributions:

Mean/@{𝒟,Histogram𝒟,KernelMixture𝒟,SmoothKernel𝒟,Empirical𝒟}

Variance/@{𝒟,Histogram𝒟,KernelMixture𝒟,SmoothKernel𝒟,Empirical𝒟}

Estimate an unknown slice distribution of a random process:

proc=ARCHProcess[.3,{.5}];

Slice distribution is unknown:

PDF[proc[2],x]

Generate a random sample of paths:

data=RandomVariate[proc[2],10^3];

Visualize its probability density function:

Histogram[data,40,"PDF"]

Test if it fits a standard normal distribution:

DistributionFitTest[data,NormalDistribution[0,1],"TestConclusion"]

OpenersExampleSubsection7(Spacer1)Random Experiments  (4)

Simulate 10 throws of a fair six-sided die:

RandomVariate[DiscreteUniformDistribution[{1,6}],10]

Simulate seven throws of a pair of fair six-sided dice:

RandomVariate[DiscreteUniformDistribution[{1,6}],{7,2}]

A coin-tossing experiment consists of tossing a fair coin repeatedly until a head results. Simulate the process:

RandomVariate[GeometricDistribution[1/2],10]

ListPlot[RandomVariate[GeometricDistribution[1/2],100],Filling->Axis]

A radioactive material on average emits 3.2 α-particles per second; show the distribution. Simulate a typical particle-count experiment for 10 minutes:

ListLinePlot[RandomVariate[PoissonDistribution[3.2],60 10]]

Simulate a symmetric random walk with values -1 and 1:

randomWalk[n_]:=FoldList[Plus,0,RandomVariate[TransformedDistribution[2u-1,u\[Distributed]BernoulliDistribution[1/2]],n-1]]

ListPlot[randomWalk[100],Joined->True]

OpenersExampleSubsection8(Spacer1)Estimation and Hypothesis Testing  (3)

Generate random data for a normal distribution:

data=RandomVariate[NormalDistribution[2,3],10^4];

Estimate the distribution parameters using the random data:

FindDistributionParameters[data,NormalDistribution[a,b]]

Generate high-precision random data for a component mixture distribution:

data=RandomVariate[MixtureDistribution[{1/5,4/5},{GammaDistribution[2,3],NormalDistribution[1,1/2]}],10^3,WorkingPrecision-> 20];

Estimate the mixture probabilities, assuming the component distributions are known:

EstimatedDistribution[data,MixtureDistribution[{p,1-p},{GammaDistribution[2,3],NormalDistribution[1,1/2]}],WorkingPrecision-> 20]

Given a binormal sample, the h-statistic follows a shifted (RefLink[FisherZDistribution, paclet:ref/FisherZDistribution]):

hStatistic[sample_]:=Module[{x,y},{x,y}=Transpose[sample];0.5 Log[Total[(x+y)^2]/Total[(x-y)^2]]]

Generate the distribution of h-statistics for binormal samples of size n:

data[ρ_,n_,m_]:=Table[hStatistic[RandomVariate[BinormalDistribution[ρ],n]],{i,m}]

Visually compare the h-statistic distribution to a shifted (RefLink[FisherZDistribution, paclet:ref/FisherZDistribution]):

𝒵𝒟[ρ_,n_]=TransformedDistribution[u+ArcTanh[ρ],u\[Distributed]FisherZDistribution[n,n]];

With[{ρ=0.3,n=25},Show[Histogram[data[ρ,n,10^4],20,"PDF"],Plot[PDF[𝒵𝒟[ρ,n],x],{x,-1,1},PlotStyle->Thick,PlotRange->All]]]

(RefLink[DistributionFitTest, paclet:ref/DistributionFitTest]) confirms the result:

With[{ρ=0.3,n=25},DistributionFitTest[data[ρ,n,10^4],𝒵𝒟[ρ,n]]]

OpenersExampleSubsection9(Spacer1)Quality  (3)

Suppose there are 5 defective items in a batch of 10 items, and 6 items are selected for testing. Simulate the number of defective items found:

RandomVariate[HypergeometricDistribution[6,5,10],20]

ListPlot[%,Filling->Axis]

Out of 10 bulbs produced, one is defective. Simulate the production of 100 bulbs:

bulbs=RandomVariate[BernoulliDistribution[9/10],100]

Find the percentage of good bulbs:

Count[bulbs,1]

Find the average number of good bulbs per batch of 100:

Mean[BernoulliDistribution[9/10]]100

Find the probability that a randomly selected bulb is good:

Probability[x==1,x\[Distributed]BernoulliDistribution[9/10]]

A shipment of products is inspected in batches of 60 and each batch is inspected up to rejection when the 10(^th) defective item is found. What is the probability of a batch being rejected if 20% of the items are defective?

Probability[x≤50\[Conditioned]x≤60,x\[Distributed]NegativeBinomialDistribution[10,0.2]]

Alternatively, compute the same result by truncating the distribution:

Probability[x≤50,x\[Distributed]TruncatedDistribution[{0,60},NegativeBinomialDistribution[10,0.2]]]

Simulate the number of non-defective products in rejected batches:

𝒟=TruncatedDistribution[{0,60},NegativeBinomialDistribution[10,0.2]];

sample=RandomVariate[𝒟,20]

Illustrate the ratio in each batch:

BarChart[Transpose[{sample,60-sample}],ChartLayout->"Stacked"]

Find the average ratio of non-defective to defective products in rejected batches:

Mean[𝒟]/Expectation[60-x,x\[Distributed]𝒟]

OpenersExampleSubsection10(Spacer1)Traffic  (3)

The number of customers arriving at a service desk follows (RefLink[PoissonDistribution, paclet:ref/PoissonDistribution]) with mean 0.6, and the number of customers already in line before the service desk opens follows (RefLink[PoissonDistribution, paclet:ref/PoissonDistribution]) with mean 5. The number of customers served until there is no one in line follows (RefLink[PoissonConsulDistribution, paclet:ref/PoissonConsulDistribution]):

gpd=PoissonConsulDistribution[5,0.6];

Plot the probability mass function:

DiscretePlot[PDF[gpd,k],{k,0,25}]

Simulate numbers of customers served at 30 busy periods:

ListPlot[{RandomVariate[gpd,30],{{0,Mean[gpd]},{30,Mean[gpd]}}},Joined->{False,True},Filling->Axis]

The average number of accidents in a city is 100 per day. Simulate the daily accidents:

RandomVariate[PoissonDistribution[100],30]

ListPlot[%,Filling->Axis]

The expected number of raindrops falling into a bucket in a five-second interval is 20. Simulate the raindrop count for each five-second interval:

RandomVariate[PoissonDistribution[20],20]

ListPlot[%,Filling->Axis]

OpenersExampleSubsection11(Spacer1)Finance  (1)

Assuming daily logarithmic return of the stock market follows a stable distribution, simulate and visualize stock prices over a period of 5 years:

logReturns=BlockRandom[SeedRandom[2010];RandomVariate[StableDistribution[1,1.38,-0.096,-0.001,0.005],5*365]];

ListLinePlot[1000Exp[Accumulate[logReturns]],AxesLabel->{Text["days"],Text["stock price"]}]

OpenersExampleSubsection12(Spacer1)Other Applications  (5)

(RefLink[RandomVariate, paclet:ref/RandomVariate]) can generate complex numbers if necessary:

RandomVariate[TransformedDistribution[x+I x^2,x\[Distributed]ExponentialDistribution[1/3]]]

Evaluate an integral numerically using a Monte Carlo scheme:

g[x_]=Sqrt[x^20+1];

Approximate with an interpolating function defined with uniformly distributed random nodes:

r=RandomVariate[UniformDistribution[],10^2];

nods=Join[Table[{x,g[x]},{x,r}],{{0,g[0]},{1,g[1]}}];

Interpolate between the nodes:

g1=Interpolation[nods]

ListPlot[{g1,nods},Joined->{True,False},PlotStyle->{Thick,Red}]

Find the value of the integral using the interpolating function:

NIntegrate[g1[x],{x,0,1}]

Compare with the value obtained using the original function:

Integrate[g[x],{x,0,1}]

N[%]

Sample a Gaussian symmetric matrix:

m=RandomVariate[GaussianOrthogonalMatrixDistribution[10^3]];

Fit (RefLink[WignerSemicircleDistribution, paclet:ref/WignerSemicircleDistribution]) to its eigenvalues:

edist=EstimatedDistribution[Eigenvalues[m],WignerSemicircleDistribution[r]]

Compare the histogram of the eigenvalues with the PDF:

Show[Histogram[Eigenvalues[m],20,"PDF"],Plot[PDF[edist,x],{x,-65,65},PlotStyle->Thick,Exclusions->None]]

The heights of females in the United States follow normal distribution with mean 64 inches and standard deviation of 2 inches, while the heights of males in the United States follow normal distribution with mean 70 inches and standard deviation of 2 inches. If the population ratio of males to females is 1:1, then the heights of the whole population have the following bimodal distribution:

height𝒟=MixtureDistribution[{0.5238,1-0.5238},{NormalDistribution[70,2],NormalDistribution[64,2]}];

Plot[PDF[height𝒟,x],{x,50,80},Filling->Axis]

Simulate the typical distribution of heights in a town of population 100:

ListPlot[RandomVariate[height𝒟,100],Filling->Axis]

Find the probability that a person is at least 73 inches tall:

Probability[x≥73,x\[Distributed]height𝒟]

A basketball player shoots free throws until he hits 4 of them. His probability of scoring in any one of them is 0.7. Simulate the process:

4+RandomVariate[NegativeBinomialDistribution[4,0.7],50]

ListPlot[%,Filling->Axis]

Find the number of shots the player is expected to shoot:

Expectation[x+4,x\[Distributed]NegativeBinomialDistribution[4,0.7]]

OpenersExampleSection4(Spacer1)Properties & Relations  (17)

(RefLink[RandomInteger, paclet:ref/RandomInteger]) generates uniform discrete random variates:

RandomInteger[{0,10},10]

(RefLink[RandomReal, paclet:ref/RandomReal]) generates uniform continuous variates:

RandomReal[{0,10},10]

(RefLink[RandomChoice, paclet:ref/RandomChoice]) generates random choices with replacement from a list:

RandomChoice[Range[10],10]

(RefLink[RandomSample, paclet:ref/RandomSample]) generates random choice without replacement from a list:

RandomSample[Range[20],10]

(RefLink[RandomPrime, paclet:ref/RandomPrime]) generates a random prime number:

RandomPrime[{1,100},10]

(RefLink[RandomImage, paclet:ref/RandomImage]) generates a random image:

RandomImage[LaplaceDistribution[0,.2],{100,100}]

(RefLink[RandomGraph, paclet:ref/RandomGraph]) generates a random graph:

RandomGraph[BarabasiAlbertGraphDistribution[100,2]]

(RefLink[RandomFunction, paclet:ref/RandomFunction]) generates a path for a random process:

𝒫=WienerProcess[3.4,4];

ListLinePlot[RandomFunction[𝒫,{0,20,0.7}]]

Use (RefLink[RandomVariate, paclet:ref/RandomVariate]) to generate a sample for a time slice of the process:

data=RandomVariate[𝒫[3],10^5];

Show[Histogram[data,20,"ProbabilityDensity"]]

Test whether the mean or median is zero by using (RefLink[LocationTest, paclet:ref/LocationTest]):

data=RandomVariate[NormalDistribution[0,1],10^2];

LocationTest[data,0,{"TestDataTable",All}]

Compare means or medians for several datasets using (RefLink[LocationEquivalenceTest, paclet:ref/LocationEquivalenceTest]):

data1=RandomVariate[NormalDistribution[.25,1],100];
data2=RandomVariate[NormalDistribution[0,1],150];
data3=RandomVariate[NormalDistribution[0,1],135];

LocationEquivalenceTest[{data1,data2,data3},{"TestDataTable",All}]

Test whether two datasets have the same variance by using (RefLink[VarianceTest, paclet:ref/VarianceTest]):

data=RandomVariate[NormalDistribution[],{2,100}];

VarianceTest[data,Automatic,{"TestDataTable",All}]

Test whether several datasets have the same variance by using (RefLink[VarianceEquivalenceTest, paclet:ref/VarianceEquivalenceTest]):

data=RandomVariate[NormalDistribution[],{3,(10^4)}];

VarianceEquivalenceTest[data,{"TestDataTable",All}]

Use (RefLink[DistributionFitTest, paclet:ref/DistributionFitTest]) to test goodness of fit between random data and a distribution:

data=RandomVariate[LaplaceDistribution[1,2],10^3];

DistributionFitTest[data,LaplaceDistribution[1,2],{"TestDataTable",All}]

Estimate distribution parameters from random data using (RefLink[EstimatedDistribution, paclet:ref/EstimatedDistribution]):

data=RandomVariate[GammaDistribution[1,3],10^5];

dist=EstimatedDistribution[data,GammaDistribution[α,β]]

Show[
Histogram[data,{0,20,0.5},"PDF"],
Plot[Evaluate@PDF[dist,x],{x,0,20},PlotStyle->Thick,PlotRange->All]]

Estimate nonparametric distributions from random data:

data=RandomVariate[NormalDistribution[],100];

{𝒟1,𝒟2,𝒟3}={EmpiricalDistribution[data],HistogramDistribution[data],SmoothKernelDistribution[data]};

{Plot[CDF[𝒟1,x],{x,-3,3},Exclusions->None,PlotLabel->"Empirical"],Plot[CDF[𝒟2,x],{x,-3,3},Exclusions->None,PlotLabel->"Histogram"],Plot[CDF[𝒟2,x],{x,-3,3},Exclusions->None,PlotLabel->"Kernel"]}

Compare random data to distributions using statistical visualization functions:

data=RandomVariate[ExponentialDistribution[2],10^3];

{QuantilePlot[data,ExponentialDistribution[λ]],ProbabilityPlot[data,ExponentialDistribution[λ]]}

Compare multiple distributions using statistical charts:

data=Table[RandomVariate[NormalDistribution[μ,1],100],{μ,{0,3,2}}];

{BoxWhiskerChart[data],DistributionChart[data]}

Plot histograms from random data:

{Histogram[RandomVariate[NormalDistribution[0,1],200]],Histogram3D[RandomVariate[BinormalDistribution[1/3],400]]}

Plot smooth kernel density estimates from data:

{SmoothHistogram[RandomVariate[NormalDistribution[0,1],200],Filling->Axis],SmoothHistogram3D[RandomVariate[BinormalDistribution[1/3],400]]}

OpenersExampleSection5(Spacer1)Possible Issues  (3)

The speed of random variate generation may depend on the distribution:

dist=NormalDistribution[];

Table[Timing[RandomVariate[TruncatedDistribution[a,dist],10^4];][[1]],{a,{{-1/20,1/20},{-1/200,1/200},{-1/2000,1/2000}}}]

The (RefLink[WorkingPrecision, paclet:ref/WorkingPrecision]) option is ignored for discrete distributions:

RandomVariate[PoissonDistribution[3],5,WorkingPrecision-> 20]

The result from (RefLink[RandomVariate, paclet:ref/RandomVariate]) may include infinite quantities:

SeedRandom[8396];data=RandomVariate[NormalDistribution[],{3,3,3},WorkingPrecision->6]

Using a different level of precision avoids the problem in this case:

SeedRandom[8396];data=RandomVariate[NormalDistribution[],{3,3,3},WorkingPrecision->7]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[SeedRandom, paclet:ref/SeedRandom])  ▪  (RefLink[BlockRandom, paclet:ref/BlockRandom])  ▪  (RefLink[RandomChoice, paclet:ref/RandomChoice])  ▪  (RefLink[RandomInteger, paclet:ref/RandomInteger])  ▪  (RefLink[RandomReal, paclet:ref/RandomReal])  ▪  (RefLink[RandomComplex, paclet:ref/RandomComplex])  ▪  (RefLink[RandomFunction, paclet:ref/RandomFunction])  ▪  (RefLink[RandomGraph, paclet:ref/RandomGraph])  ▪  (RefLink[RandomImage, paclet:ref/RandomImage])  ▪  (RefLink[RandomInstance, paclet:ref/RandomInstance])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Random Number Generation, paclet:guide/RandomNumberGeneration])}, {▪(RefLinkPlain[Random Variables, paclet:guide/RandomVariables])}, {▪(RefLinkPlain[Probability & Statistics, paclet:guide/ProbabilityAndStatistics])}, {▪(RefLinkPlain[Matrix Distributions, paclet:guide/MatrixDistributions])}, {▪(RefLinkPlain[Matrices and Linear Algebra, paclet:guide/MatricesAndLinearAlgebra])}, {▪(RefLinkPlain[Probability & Statistics with Quantities, paclet:guide/ProbabilityWithQuantities])}, {▪(RefLinkPlain[Scientific Models, paclet:guide/ScientificModels])}, {▪(RefLinkPlain[Statistical Data Analysis, paclet:guide/Statistics])}, {▪(RefLinkPlain[Random Graphs, paclet:guide/RandomGraphs])}, {▪(RefLinkPlain[Reliability, paclet:guide/Reliability])}, {▪(RefLinkPlain[Actuarial Computation, paclet:guide/ActuarialComputation])}})}})