Sample - Maple Help

Statistics

 Sample
 generate random sample

 Calling Sequence Sample(X, n, opts) Sample(X, m, opts) Sample(X, rng, opts) Sample(X, out, opts) Sample(X, opts)

Parameters

 X - algebraic; random variable or distribution n - nonnegative integer; sample size m - list of two nonnegative integers; Matrix dimensions rng - integer range or list of integer ranges; Array dimensions out - float rtable; to be filled with data options - (optional) equations of the form option = value, where option is method, possibly indexed by a name; specify options for sample generation

Options

 • method = name or method = list -- This option can be used to select a method of generating the sample. There are four main choices: method = default, method = custom, method = discrete, and method = envelope. One can supply method-specific options by instead specifying a list, the first element of which is one of the names default, custom, discrete, and envelope, and the other elements are equations; for example, method = [envelope, updates=20, range=0..100]. These method-specific options will be explained below.
 – method = envelope uses an implementation of acceptance/rejection generation with an adaptive piecewise linear envelope, applicable to continuous distributions. This implementation will only work for distributions where on its support, the PDF is twice differentiable, has a continuous first derivative, and has only finitely many inflection points. There are three valid method-specific options: range, basepoints, and updates.
 range: The (finite) range over which the piecewise linear envelope is to be defined, and consequently where the samples are to be found. If range = deduce (the default), then Maple takes the range given by the $\mathrm{\epsilon }$ and $1-\mathrm{\epsilon }$ Quantiles of the distribution, for some small positive value of $\mathrm{\epsilon }$ that depends on the value of Digits. Otherwise, range should be a range of two real numbers, such as range = 0 .. 1.
 basepoints: The base points are the boundaries between the segments of the piecewise linear envelope, which should include all inflection points of the PDF of the distribution. If basepoints = deduce (the default), then Maple attempts to find all inflection points itself. Otherwise, basepoints should be a list of floating point real numbers which includes all inflection points.
 updates: The envelope is automatically refined as more numbers are generated; the maximal number of segments is given by this option, which should be a positive integer. The default value is $100$.
 – method = discrete uses an implementation of the alias method by Walker (see references below), applicable to discrete distributions. Because this method computes and stores the individual probabilities for all possible outcomes within the range (see below), it may be inefficient for distributions with very heavy tails. There is one method-specific option: range.
 range: The (finite) range of integers for which the probabilities are computed. If the distribution uses the DiscreteValueMap feature (this is the case if the distribution can attain non-integer values), then this describes the range of source values; the map is applied to these integers to obtain the resulting values.
 – method = custom uses a distribution-specific method. Almost all predefined distributions have a highly efficient custom implementation in external C code. Method-specific options are all ignored.
 – method = default (which is the default) selects one of the other three methods. For most built-in distributions, it selects method = custom. For other distributions, such as custom-defined ones, the system falls back to either using method = envelope (for continuous distributions) or using method = discrete (for discrete distributions). The method-specific options accepted are the same as for the applicable fallback method, and they are only used in case the system falls back to that generator.
 • If X is an algebraic expression involving multiple random variables, say ${R}_{1}$ and ${R}_{2}$, then one can specify different sample generation methods for ${R}_{1}$ and ${R}_{2}$ by using options ${\mathrm{method}}_{{R}_{1}}={\mathrm{generator}}_{1}$ and ${\mathrm{method}}_{{R}_{2}}={\mathrm{generator}}_{2}$, where ${\mathrm{generator}}_{1}$ and ${\mathrm{generator}}_{2}$ are sample generation methods that could be validly specified as $\mathrm{method}={\mathrm{generator}}_{i}$. If a random variable-specific sample generation method is given only for some of the random variables, the others will use the method given by the $\mathrm{method}=\mathrm{...}$ option, or default if no such option is present.

Efficiency

 • When implementing an algorithm that uses a large number of random samples, it can be worthwhile to think about efficiency of the random sample generation. In most cases, the best efficiency is achieved when all samples are generated at once in a preprocessing phase and stored in a Vector (using the first calling sequence, above), and the values are then used one by one in the algorithm.
 In some cases, however, this is not possible. For example, this might take too much memory (if a very large number of samples is needed), it might be difficult or impossible to predict the number of samples needed, or the parameters of the random variable might change during the algorithm. In the first two cases, the recommended strategy is to use the fourth calling sequence to create a procedure p, then use p to create a Vector that can hold a large number of samples (using, say, $v≔p\left({10}^{5}\right)$), using the elements of v one by one, and calling $p\left(v\right)$ to refill v when the samples run out. If the parameters of the random variable keep changing, then one can define the random variable with parameters that are unassigned initially, use the fourth calling sequence to create a procedure p, then assign values to the parameters afterwards. An example is provided below.
 • For some of the discrete distributions, the method selected by default is not method = custom but method = discrete. For these distributions, this method is faster when generating more than about 1000 random numbers. If you need to generate fewer random numbers, you can select method = custom by including that option explicitly.

Description

 • The Sample command generates a random sample drawn from the distribution given by X.
 • The first parameter, X, can be a distribution (see Statistics[Distribution]), a random variable, or an algebraic expression involving random variables (see Statistics[RandomVariable]).
 • In the first calling sequence, the second parameter, n, is the sample size. This calling sequence will return a newly created Vector of length n, filled with the sample values. This calling sequence, or one of the next two, is recommended for all cases where there are no great performance concerns.
 • In the second calling sequence, the second parameter, m, is a list of two nonnegative integers. This calling sequence will return a newly created Matrix with the specified dimensions, filled with the sample values.
 • In the third calling sequence, the second parameter, rng, is a range or a list of ranges determining the dimensions of an Array. This Array will be created, filled with the sample values, and returned.
 • In the fourth calling sequence, the second parameter, out, is an rtable (such as a Vector) that was created beforehand. Upon successful return of the Sample command, out will have been filled with the sample values.
 out needs to have rectangular storage and the float data type that is consistent with the current settings of Digits and UseHardwareFloats. That is, if either UseHardwareFloats = true, or UseHardwareFloats = deduced and Digits <= evalhf(Digits) (which is the default), then out needs to have datatype = float[8]; in the other case, that is, if either UseHardwareFloats = false, or UseHardwareFloats = deduced and Digits > evalhf(Digits), then out needs to have datatype = sfloat. This can easily be achieved by supplying the option datatype = float to the rtable creation function; this will automatically select the correct data type for the current settings.
 • In the fourth calling sequence, Sample returns a procedure p, which can subsequently be called to generate samples of X repeatedly. The procedure p accepts a single argument, which can be n, m, rng, or out, and then behaves as if one of the first three calling sequences were called. p does not accept options; any options should be given in the call to Sample itself.

Examples

 > $\mathrm{with}\left(\mathrm{Statistics}\right):$

Straightforward sampling of a distribution.

 > $X≔\mathrm{RandomVariable}\left(\mathrm{Normal}\left(0,1\right)\right)$
 ${X}{≔}{\mathrm{_R}}$ (1)
 > $A≔\mathrm{Sample}\left(X,{10}^{6}\right):$
 > $P≔\mathrm{DensityPlot}\left(X,\mathrm{range}=-2..2,\mathrm{thickness}=3,\mathrm{color}=\mathrm{red}\right):$
 > $Q≔\mathrm{Histogram}\left(A,\mathrm{range}=-2..2\right):$
 > $\mathrm{plots}\left[\mathrm{display}\right]\left(P,Q\right)$

We can also sample an expression involving two random variables.

 > $Y≔\mathrm{RandomVariable}\left(\mathrm{Normal}\left(0,1\right)\right)$
 ${Y}{≔}{\mathrm{_R0}}$ (2)
 > $B≔\mathrm{Sample}\left(\frac{X}{Y},{10}^{6}\right):$
 > $P≔\mathrm{DensityPlot}\left(\mathrm{Cauchy}\left(0,1\right),\mathrm{range}=-2..2,\mathrm{thickness}=3,\mathrm{color}=\mathrm{red}\right):$
 > $Q≔\mathrm{Histogram}\left(B,\mathrm{range}=-2..2\right):$
 > $\mathrm{plots}\left[\mathrm{display}\right]\left(P,Q\right)$

Sampling of a custom-defined distribution.

 > $\mathrm{dist}≔\mathrm{Distribution}\left(\mathrm{=}\left(\mathrm{PDF},t↦\mathrm{piecewise}\left(t<0,0,t<1,\frac{36\cdot {t}^{2}\cdot \left(\frac{4}{3}-t\right)}{7},0\right)\right)\right)$
 ${\mathrm{dist}}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Distribution}}{,}{\mathrm{Continuous}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{PDF}}{,}{\mathrm{Conditions}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (3)
 > $B≔\mathrm{Sample}\left(\mathrm{RandomVariable}\left(\mathrm{dist}\right),{10}^{6}\right):$
 > $P≔\mathrm{DensityPlot}\left(\mathrm{dist},\mathrm{range}=0..1,\mathrm{thickness}=3,\mathrm{color}=\mathrm{red}\right):$
 > $Q≔\mathrm{Histogram}\left(B\right):$
 > $\mathrm{plots}\left[\mathrm{display}\right]\left(P,Q\right)$

If we supply a list of ranges instead of a number, we get an Array. With a list of two numbers, we get a Matrix.

 > $a≔\mathrm{Sample}\left(\frac{X}{Y},\left[1..1000,1..1000\right]\right)$
 ${a}{≔}\begin{array}{c}\left[\begin{array}{ccccccccccc}{1.25172296801703}& {1.61112270486496}& {0.499416606097209}& {3.44567592190394}& {-1.53337626111541}& {-0.996448674775313}& {-1.04610082346673}& {0.685851042569387}& {1.95971911721693}& {0.156045629166316}& {\dots }\\ {1.10943069591670}& {-11.3114062785426}& {2.57543828888686}& {0.143186043780654}& {2.38195548133515}& {0.145292851062041}& {1.89928617451768}& {0.644467922342911}& {-3.71510294021439}& {35.7542117785261}& {\dots }\\ {-2.20701019863751}& {-0.728833510885991}& {-0.602188166120584}& {0.0723890973978941}& {0.628358129954596}& {0.168360186996424}& {2.10147949766641}& {121.551090686724}& {-0.276444875704007}& {-5.22859012506017}& {\dots }\\ {0.561344741364804}& {-31.1256682514379}& {-2.31526460631566}& {-0.880281510124309}& {-0.200096402350086}& {0.741171077327066}& {-1.87897313556876}& {-6.97294135411677}& {-0.274165317903244}& {0.991661836692721}& {\dots }\\ {-5.82989550245340}& {3.03948229789449}& {-35.6482331934652}& {0.0575911982621098}& {0.0605340651880884}& {-0.0483602918661156}& {9.44142218718587}& {-0.0453415590539228}& {-0.643468852565026}& {-11.0138448795560}& {\dots }\\ {-1.28796788570459}& {0.0942098544417537}& {1.28782098238736}& {3.04241449498299}& {0.350957121877448}& {-0.132574733744440}& {1.14271477132740}& {0.537739562211219}& {0.689286433354773}& {-0.136887004625812}& {\dots }\\ {-1.12115262528574}& {-6.45759657121362}& {0.0835561097178314}& {-2.10158258305500}& {-0.399860608647929}& {-1.13561663220238}& {-1.05574568627504}& {0.221160881294326}& {-1.24669080492581}& {-0.999355712981057}& {\dots }\\ {-0.304363739485778}& {0.449991357257201}& {0.945188305733328}& {1.39210041060987}& {0.0491027119495798}& {-0.164923010879633}& {1.65594382566873}& {47.4579856448656}& {-0.993413814447978}& {12.4750246474787}& {\dots }\\ {-1.37424213198316}& {7.84370619517727}& {1.41818044842362}& {1.25273187531079}& {-0.592597950434855}& {0.749909855853181}& {-1.06791758631215}& {3.45254708115992}& {0.772088352288990}& {-2.17754995300465}& {\dots }\\ {0.230070036479805}& {-0.436962381172013}& {-26.2637591161311}& {0.394779527220718}& {0.440685142231469}& {-1.37205984540469}& {1.26027933482401}& {-1.84262532839157}& {-2.91067436105332}& {-0.887578917861802}& {\dots }\\ {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {}\end{array}\right]\\ \hfill {\text{1000 × 1000 Array}}\end{array}$ (4)
 > $m≔\mathrm{Sample}\left(\frac{X}{Y},\left[3,3\right]\right)$
 ${m}{≔}\left[\begin{array}{ccc}{1.73381095915784}& {0.619445146766988}& {-4.38544454374716}\\ {-3.65149550630508}& {-0.940625211071576}& {1.15063333773690}\\ {-6.27347263502003}& {2.04859402579226}& {-0.234438105073932}\end{array}\right]$ (5)

We can use envelope rejection sampling to restrict $X$ and $Y$ to a certain range.

 > $\mathrm{s1}≔\mathrm{Sample}\left(\frac{X}{Y},{10}^{6},\mathrm{method}=\left[\mathrm{envelope},\mathrm{range}=0..1\right]\right)$
 ${\mathrm{s1}}{≔}\left[{0.164509105308204}{,}{4.09581730657051}{,}{0.953947579812038}{,}{1.12682770977962}{,}{0.0703922155159162}{,}{1.09046346214752}{,}{1.94678223266721}{,}{1.27337159146991}{,}{0.241513499604467}{,}{0.295905398800741}{,}{2.31015210657094}{,}{0.616692433507840}{,}{1.10412558481734}{,}{2.21177309943416}{,}{14.0573202605532}{,}{0.324501445788778}{,}{0.273924429181846}{,}{2.00133043546671}{,}{5.61464411183888}{,}{0.804653340953734}{,}{2.50541673688355}{,}{0.988881057168882}{,}{0.852470948110485}{,}{0.255216183422600}{,}{5.45079705526982}{,}{0.00928018500158662}{,}{1.65041376482505}{,}{0.316838869936501}{,}{3.38998414882937}{,}{0.195926264566680}{,}{0.251333007057218}{,}{1.48682768637748}{,}{1.21994257253521}{,}{0.981982204865467}{,}{0.922477493137259}{,}{0.269250826492342}{,}{0.0691095109372808}{,}{2.50388041838378}{,}{1.93999332498434}{,}{1.32418054913374}{,}{10.9121191017004}{,}{2.29652704094047}{,}{81.2153747168618}{,}{0.127445499463890}{,}{8.91856664687122}{,}{0.0633799734183104}{,}{0.901723041211533}{,}{2.32629944823613}{,}{0.171765748877928}{,}{0.861943178576114}{,}{0.693381278185530}{,}{0.0541563809199750}{,}{0.600580940298313}{,}{0.455838886156625}{,}{1.39269775774786}{,}{1.16169483097379}{,}{0.916416314486803}{,}{0.628562718751338}{,}{1.18254236370235}{,}{1.09408927005331}{,}{0.373111544014444}{,}{1.29214015251514}{,}{0.953729750200040}{,}{1.12972380931807}{,}{0.238597857689066}{,}{3.51487506808630}{,}{0.0605604006514988}{,}{0.645120272833133}{,}{11.5705336165668}{,}{1.58083445137093}{,}{0.924485354904197}{,}{0.0633042249868064}{,}{50.6390225208395}{,}{0.369898743791454}{,}{0.885926639691173}{,}{0.495699054709186}{,}{4.35953319251560}{,}{0.471063721862322}{,}{0.0748695075226005}{,}{1.77467656635627}{,}{0.793722005960731}{,}{0.649982979351702}{,}{0.0798528833286454}{,}{1.28107253263137}{,}{2.85611419512204}{,}{2.12477533204076}{,}{0.513748517815970}{,}{0.629819885481221}{,}{3.42546622148552}{,}{0.0342547379162728}{,}{2.38390292396541}{,}{0.219934520726131}{,}{0.165679871835078}{,}{0.940236684817206}{,}{1.09901850402721}{,}{1.45309516148122}{,}{1.27273250225991}{,}{0.270431370427649}{,}{0.720131509323273}{,}{6.14354367725056}{,}{\dots }{,}{\text{⋯ 999900 row vector entries not shown}}\right]$ (6)
 > $\mathrm{Histogram}\left(\mathrm{s1},\mathrm{range}=0..10\right)$

Or to restrict only $X$ to a certain range.

 > $\mathrm{s2}≔\mathrm{Sample}\left(\frac{X}{Y},{10}^{6},\mathrm{method}\left[X\right]=\left[\mathrm{envelope},\mathrm{range}=0..1\right]\right)$
 ${\mathrm{s2}}{≔}\left[{0.151521094342855}{,}{0.478856589441737}{,}{1.02822429825999}{,}{0.319722419885092}{,}{1.02356638359008}{,}{0.473187261873492}{,}{-1.29596307784062}{,}{-0.801915227053641}{,}{-0.223332829253985}{,}{-0.424247641203059}{,}{-0.0875831656324448}{,}{0.599234575205733}{,}{1.08758541828342}{,}{0.533613884707065}{,}{-0.0179935608271068}{,}{-135.567279794557}{,}{-1.65291049904415}{,}{0.130899941479875}{,}{2.35009423458567}{,}{-0.973536417778484}{,}{0.279169378420664}{,}{-2.85349780439734}{,}{-0.00453241915198974}{,}{-0.00979481249529829}{,}{-0.597988981850376}{,}{0.137112703115549}{,}{-0.699567352171680}{,}{-0.251066626217017}{,}{0.231403901396141}{,}{-0.273099682258672}{,}{-1.00188480145633}{,}{-28.9821982311154}{,}{-2.89839015202163}{,}{-2.47406858262032}{,}{0.484828316792614}{,}{0.387535109944948}{,}{-1.16620395453305}{,}{1.36930042448478}{,}{0.272016835994672}{,}{-0.413144776525714}{,}{2.93470594802699}{,}{-1.03167086142809}{,}{1.01069634615967}{,}{0.479812038538215}{,}{2.30913903229284}{,}{0.178236548861489}{,}{9.60727504992754}{,}{0.440264727788961}{,}{0.656739999404844}{,}{1.93317826290210}{,}{-0.519627946954369}{,}{27.8390651095191}{,}{0.434380693631781}{,}{0.372381915105012}{,}{-0.342221029792477}{,}{-0.525498578409414}{,}{0.654294741112703}{,}{-0.351104673963768}{,}{-3.53651026259870}{,}{0.531445079481273}{,}{-0.326090306833127}{,}{-0.526246628528327}{,}{15.0185044935097}{,}{-0.0585922000175793}{,}{-0.122427089068340}{,}{23.2003068750719}{,}{-8.33510907291873}{,}{-27.5628841660846}{,}{-0.0674460212259268}{,}{-0.700000710352142}{,}{-0.973175080790548}{,}{0.250945620940322}{,}{0.345230252697525}{,}{-0.565186621301109}{,}{-0.199211976298712}{,}{-0.0549945106667275}{,}{-1.56831429881440}{,}{-0.151680901818712}{,}{-2.78587698881289}{,}{-4.28935800409278}{,}{-0.720144794912751}{,}{-17.9345788247907}{,}{-0.696539912256034}{,}{0.223254557715554}{,}{-0.678655051062050}{,}{-0.219457236909401}{,}{-0.523144558205263}{,}{-0.338883473757903}{,}{0.179721549011317}{,}{0.315071162160934}{,}{1.17263268707613}{,}{0.271164859952502}{,}{1.95644664663684}{,}{1.25145220931139}{,}{0.0256823955634587}{,}{-0.177824164576651}{,}{-1.92434919835408}{,}{-0.726634338637498}{,}{-0.142267494389816}{,}{-0.363296186515732}{,}{\dots }{,}{\text{⋯ 999900 row vector entries not shown}}\right]$ (7)
 > $\mathrm{Histogram}\left(\mathrm{s2},\mathrm{range}=-10..10\right)$

We can refill $\mathrm{s2}$ with different samples as follows.

 > $\mathrm{Sample}\left(\mathrm{Cauchy}\left(0,1\right),\mathrm{s2}\right)$
 $\left[{2.53843996582646}{,}{-0.0153325435991015}{,}{0.608857509582078}{,}{-0.320316153413050}{,}{1.39156189704278}{,}{0.0523606981995763}{,}{0.507464357462665}{,}{-0.462840103444002}{,}{-0.601690620848121}{,}{12.7803236259751}{,}{-0.0195776687397458}{,}{-4.63681324037527}{,}{0.148431471133466}{,}{-2.85118674664208}{,}{-0.988271081284889}{,}{-0.598504466825700}{,}{-4.46166842532879}{,}{20.8633190607472}{,}{-6.54500670162488}{,}{-0.605885621903898}{,}{0.251488120519590}{,}{-0.0763654309134204}{,}{-3.90687438528318}{,}{-8.44613469709219}{,}{0.996318239512978}{,}{-1.40451437108476}{,}{0.245564074934898}{,}{1.18797649381696}{,}{-6.58674018916877}{,}{-3.44054346786835}{,}{-1.82146434407694}{,}{3.39528482195936}{,}{19.5425668253970}{,}{0.461866089662991}{,}{-3.60793455254018}{,}{0.571479971836530}{,}{0.162752251344559}{,}{-0.619936146442429}{,}{-0.814030448530102}{,}{-2.46281596238424}{,}{0.817575434123563}{,}{0.709912561146836}{,}{-8.33021499087266}{,}{27.3807070325521}{,}{-1.29624436764500}{,}{0.0397831242550352}{,}{0.228272932057817}{,}{0.864669795493779}{,}{0.504233611893208}{,}{-0.764210550283776}{,}{2.75539066320200}{,}{-0.264592832088236}{,}{-0.0495901206976135}{,}{0.494455479350471}{,}{0.371272068061239}{,}{-0.768064174910420}{,}{0.586496325308698}{,}{0.276141391159739}{,}{-0.181648563638915}{,}{-1.68233984842801}{,}{0.335351387839797}{,}{2.26814964629446}{,}{0.326802116038038}{,}{109.458852964223}{,}{0.801042039809947}{,}{-0.551174740947834}{,}{0.517494384484781}{,}{0.123600433259359}{,}{4.36131164617252}{,}{0.301923639300681}{,}{-0.528080978586568}{,}{-3.15525739421567}{,}{-0.438618709489156}{,}{-1.07287178797278}{,}{7.40833052216452}{,}{1.26304564889564}{,}{-3.15329929490717}{,}{0.416300599583017}{,}{1.00008129118167}{,}{1.81006506088115}{,}{-1.07989965296400}{,}{-0.205673458205986}{,}{0.0384428010432859}{,}{-1.51475956017359}{,}{0.183807484040890}{,}{-0.917657261268151}{,}{0.0837585517687177}{,}{1.78755749845944}{,}{0.293382082134718}{,}{0.223027848925820}{,}{0.311591901923752}{,}{0.259235168524433}{,}{-0.0695213686247843}{,}{0.0115307302780131}{,}{-0.814360970909812}{,}{0.359405211558193}{,}{-0.702159492990225}{,}{0.653611020130936}{,}{-0.482620617987656}{,}{0.466590588732156}{,}{\dots }{,}{\text{⋯ 999900 row vector entries not shown}}\right]$ (8)
 > $\mathrm{Histogram}\left(\mathrm{s2},\mathrm{range}=-10..10\right)$

Another option is to use a procedure.

 > $p≔\mathrm{Sample}\left(X\right)$
 ${p}{≔}{\mathbf{proc}}\left({n}{::}{\mathrm{Sample}}{:-}{\mathrm{sizeType}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (9)
 > $p\left(\mathrm{s2}\right)$
 $\left[{-0.377658773080190}{,}{0.0560845955528005}{,}{-0.117004695239357}{,}{1.08200102221046}{,}{0.702359490491469}{,}{0.0873876217062797}{,}{0.598210385561149}{,}{-0.762424673477284}{,}{-0.495111369401032}{,}{-0.318459809046205}{,}{-1.71705067440706}{,}{-0.0477062997503621}{,}{-2.43401206949785}{,}{-0.318828347642309}{,}{-0.829700990408761}{,}{0.123897921973421}{,}{0.195814104511780}{,}{-0.592616028604541}{,}{1.30282807732256}{,}{0.721543097201672}{,}{-0.679013672491204}{,}{-0.0530788770658103}{,}{1.06794530959636}{,}{1.57120434305532}{,}{-2.04514868999716}{,}{0.854868449978266}{,}{0.105571839313501}{,}{-1.11334479050827}{,}{0.725565621425632}{,}{1.09331699701913}{,}{-0.572686809452088}{,}{-0.970434818907874}{,}{0.0382884645966174}{,}{-1.41487592531766}{,}{1.51413730256480}{,}{-0.525145167615095}{,}{-0.896521773505404}{,}{0.310300425079038}{,}{0.411768642109398}{,}{0.0719041407681484}{,}{0.293434017831378}{,}{0.211632835477418}{,}{0.950978487944613}{,}{-0.911297449374679}{,}{-0.401779050730486}{,}{1.00920042811228}{,}{1.10637723779169}{,}{-0.325468129746447}{,}{-1.08397927126992}{,}{-0.0770563894484418}{,}{0.920481142671896}{,}{-0.470875420619749}{,}{-0.0211970155838697}{,}{0.477670595890787}{,}{-0.756206970419928}{,}{-0.187761108995993}{,}{0.166156209721227}{,}{1.72345133928523}{,}{-0.595034509118674}{,}{1.17433569548549}{,}{-0.336235193716987}{,}{-1.08550122349622}{,}{-1.65881694252757}{,}{-1.03796665862033}{,}{0.788811025560479}{,}{0.454665314450297}{,}{-0.895018399155396}{,}{-0.842965363094641}{,}{-0.331616125599980}{,}{-0.665963221344212}{,}{0.475585240583151}{,}{-0.728813713033104}{,}{2.00464532234509}{,}{0.0544222363886172}{,}{1.02942189608673}{,}{-1.07393336772347}{,}{0.574328164532920}{,}{-0.582488881312634}{,}{-0.147136398727831}{,}{-1.42388488901347}{,}{-0.883088524061567}{,}{0.681465589197535}{,}{-0.192705631640748}{,}{0.132564985179821}{,}{0.153165537208518}{,}{0.353176796600027}{,}{-2.93951493930740}{,}{-1.72211149835084}{,}{-0.657230533663111}{,}{0.429696887539980}{,}{0.684449117008611}{,}{2.30072707675221}{,}{0.311837470340841}{,}{-0.534935240396458}{,}{-0.0773224797082375}{,}{-0.694932186293626}{,}{-0.750174720031666}{,}{0.258249371258047}{,}{-1.11497218943226}{,}{0.0230446926929038}{,}{\dots }{,}{\text{⋯ 999900 row vector entries not shown}}\right]$ (10)
 > $\mathrm{Histogram}\left(\mathrm{s2}\right)$

Sampling of a custom-defined discrete distribution with non-integer values. This distribution attains the value ${\left(\frac{3}{2}\right)}^{n}$ with probability ${2}^{-n}$ for positive $n$.

 > $\mathrm{dist2}≔\mathrm{Distribution}\left(\mathrm{Type}=\mathrm{discrete},\mathrm{=}\left(\mathrm{ProbabilityFunction},t↦{2}^{-\mathrm{log}\left[\frac{3}{2}\right]\left(t\right)}\right),\mathrm{=}\left(\mathrm{DiscreteValueMap},n↦{\left(\frac{3}{2}\right)}^{n}\right),\mathrm{Support}=1..\mathrm{\infty }\right)$
 ${\mathrm{dist2}}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Distribution}}{,}{\mathrm{Discrete}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{ProbabilityFunction}}{,}{\mathrm{Support}}{,}{\mathrm{Conditions}}{,}{\mathrm{DiscreteValueMap}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (11)
 > $s≔\mathrm{Sample}\left(\mathrm{dist2},{10}^{5}\right)$
 ${s}{≔}\left[{1.50000000000000}{,}{1.50000000000000}{,}{7.59375000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{3.37500000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{2.25000000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{25.6289062500000}{,}{3.37500000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{2.25000000000000}{,}{2.25000000000000}{,}{2.25000000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{3.37500000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{3.37500000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{57.6650390625000}{,}{1.50000000000000}{,}{3.37500000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{2.25000000000000}{,}{2.25000000000000}{,}{2.25000000000000}{,}{3.37500000000000}{,}{1.50000000000000}{,}{7.59375000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{3.37500000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{5.06250000000000}{,}{1.50000000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{2.25000000000000}{,}{11.3906250000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{2.25000000000000}{,}{2.25000000000000}{,}{3.37500000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{7.59375000000000}{,}{5.06250000000000}{,}{3.37500000000000}{,}{7.59375000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{5.06250000000000}{,}{5.06250000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{2.25000000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{2.25000000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{2.25000000000000}{,}{3.37500000000000}{,}{1.50000000000000}{,}{11.3906250000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{17.0859375000000}{,}{3.37500000000000}{,}{2.25000000000000}{,}{1.50000000000000}{,}{3.37500000000000}{,}{1.50000000000000}{,}{1.50000000000000}{,}{11.3906250000000}{,}{1.50000000000000}{,}{\dots }{,}{\text{⋯ 99900 row vector entries not shown}}\right]$ (12)
 > $\mathrm{sort}\left(\mathrm{Tally}\left(s\right)\right)$
 $\left[{1.50000000000000}{=}{49902}{,}{2.25000000000000}{=}{25074}{,}{3.37500000000000}{=}{12481}{,}{5.06250000000000}{=}{6226}{,}{7.59375000000000}{=}{3129}{,}{11.3906250000000}{=}{1618}{,}{17.0859375000000}{=}{772}{,}{25.6289062500000}{=}{378}{,}{38.4433593750000}{=}{226}{,}{57.6650390625000}{=}{89}{,}{86.4975585937500}{=}{47}{,}{129.746337890625}{=}{26}{,}{194.619506835938}{=}{17}{,}{291.929260253906}{=}{8}{,}{437.893890380859}{=}{4}{,}{985.261253356934}{=}{1}{,}{7481.82764267921}{=}{2}\right]$ (13)

Finally, here is a somewhat longer example, where we want to generate exponentially distributed numbers; the rate parameter $\mathrm{\lambda }$ starts as being $1$, but for each subsequent value it is the square root of the previous sample value. In order to be able to use a procedure (important for efficiency), we need to make sure that $\mathrm{\lambda }$ is not defined when we create the procedure, otherwise it will only generate samples for the value that $\mathrm{\lambda }$ had at the time of definition. (If $\mathrm{\lambda }$ has a value, it can be undefined by executing lambda := 'lambda';, but since we have not used $\mathrm{\lambda }$ yet, that should not be necessary in this case.)

 > $X≔\mathrm{RandomVariable}\left(\mathrm{Exponential}\left(\mathrm{\lambda }\right)\right)$
 ${X}{≔}{\mathrm{_R6}}$ (14)
 > $p≔\mathrm{Sample}\left(X\right)$
 ${p}{≔}{\mathbf{proc}}\left({n}{::}{\mathrm{Sample}}{:-}{\mathrm{sizeType}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (15)

If we now compute a sample of $X$, then Maple will complain, because $\mathrm{\lambda }$ is unassigned:

 > $p\left(1\right)$

Instead, we assign $1$ to $\mathrm{\lambda }$ and start an iteration.

 > $\mathrm{\lambda }≔1$
 ${\mathrm{\lambda }}{≔}{1}$ (16)
 > $N≔{10}^{3}$
 ${N}{≔}{1000}$ (17)
 > $v≔\mathrm{Vector}\left(N,'\mathrm{datatype}'='\mathrm{float}'\right)$
 ${v}{≔}\begin{array}{c}\left[\begin{array}{c}{0.}\\ {0.}\\ {0.}\\ {0.}\\ {0.}\\ {0.}\\ {0.}\\ {0.}\\ {0.}\\ {0.}\\ {⋮}\end{array}\right]\\ \hfill {\text{1000 element Vector[column]}}\end{array}$ (18)
 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}i\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{to}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}N\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}v\left[i\right]≔p\left(1\right)\left[1\right];\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{\lambda }≔\mathrm{sqrt}\left(v\left[i\right]\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}:$

We now create a point plot where pairs of subsequent samples are the horizontal and vertical coordinates.

 > $\mathrm{plots}\left[\mathrm{pointplot}\right]\left(⟨v\left[..-2\right]|v\left[2..\right]⟩,\mathrm{axis}=\left[\mathrm{mode}=\mathrm{log}\right],\mathrm{symbolsize}=1,\mathrm{color}=\mathrm{red}\right)$

References

 Stuart, Alan, and Ord, Keith. Kendall's Advanced Theory of Statistics. 6th ed. London: Edward Arnold, 1998. Vol. 1: Distribution Theory.
 Walker, Alastair J. New Fast Method for Generating Discrete Random Numbers with Arbitrary Frequency Distributions, Electronic Letters, 10, 127-128.
 Walker, Alastair J. An Efficient Method for Generating Discrete Random Variables with General Distributions, ACM Trans. Math. Software, 3, 253-256.

Compatibility

 • The rng and out parameters were introduced in Maple 15.
 • The method option was introduced in Maple 15.