binornd — Generate binomially distributed random samples.
binornd draws from binomial distributions with scalar parameter expansion, explicit shape forms, deterministic RunMat random-state integration, and validated provider residency.
Syntax
r = binornd(n, p)
r = binornd(n, p, sz)
r = binornd(n, p, sz1, sz2, ...)Inputs
n- Type
NumericArray- Required
- Yes
- Description
- Positive integer trial counts supplied as dense real single or double values.
p- Type
NumericArray- Required
- Yes
- Description
- Success probabilities in the inclusive interval from zero to one.
sz- Type
SizeArg- Required
- Yes
- Description
- Output size as one scalar or one row vector.
sz1, sz2, ...- Type
SizeArg- Required
- Variadic
- Description
- Output dimension sizes supplied as separate scalar values.
Returns
r- Type
NumericArray- Description
- Binomially distributed samples with the requested shape.
Errors
RunMat:binornd:InvalidArgument- When
- Parameters or size controls are missing, unsupported, outside their domains, or shape-incompatible.
- Message
binornd: invalid argument
RunMat:binornd:Internal- When
- Random-state access, allocation, provider gather, or output restoration fails.
- Message
binornd: internal error
RunMat:binornd:TooManyOutputs- When
- More than one output is requested.
- Message
binornd: too many output arguments
Parameters and output shape
binornd(n,p) draws independent binomial samples. Each value of n must be a positive integer trial count, and each probability in p must be between zero and one, inclusive. A scalar parameter expands to the shape of the other parameter; two nonscalar parameters must have matching shapes.
Without size controls, the output follows the scalar-expanded parameter shape. A single scalar size creates a square matrix. A row-vector size or separate scalar dimensions selects an explicit shape, which must match every nonscalar parameter. Nonpositive dimensions produce an empty array, and trailing singleton dimensions beyond the second are ignored.
Classes and RunMat extensions
The documented parameter and size classes are dense real single and double. If either distribution parameter is single, the result is single; otherwise it is double. Size controls select shape only.
RunMat compatibility mode also accepts all eight fixed-width integer classes for either parameter and for size controls, plus logical values for n or p. Integer parameters must be exactly representable at the binary64 sampling boundary. Integer size values are decoded directly from authoritative storage.
Random state, provider arrays, and distributed arrays
binornd uses RunMat's shared deterministic random state. Calling rng("default") resets the sequence used by later calls.
Floating provider parameters preserve output class and residency when their exact owner can represent the result. Sampling currently uses the host RNG and restores the result through that owner. Explicit provider residency must be preserved or the call fails.
Distributed parameter arrays are materialized before sampling, so the returned array is host-resident. Values and documented scalar-expansion behavior are preserved across that boundary.
Examples
Generate a matrix of binomial samples
rng("default");
r = binornd(10, 0.5, 2, 3)Expected output:
r is a 2-by-3 double matrix with values from 0 through 10Use several trial counts and probabilities
rng("default");
n = [2 4 6 8];
p = [0.2 0.4 0.6 0.8];
r = binornd(n, p)Expected output:
r follows the 1-by-4 parameter shapeGenerate native single-precision samples
rng("default");
r = binornd(10, single([0.2 0.5 0.8]))Expected output:
r is a 1-by-3 single arrayUse deterministic probability endpoints
rng("default");
r = binornd([3 5], [0 1])Expected output:
r is [0 5]Use fixed-width parameters and sizes
rng("default");
r = binornd(uint16(8), uint8(1), uint16([2 2]))Expected output:
r is a 2-by-2 double matrix containing 8Preserve explicit provider residency
rng("default");
p = gpuArray(single([0.2 0.5 0.8]));
gr = binornd(10, p);
r = gather(gr)Expected output:
gr remains a single-precision gpuArrayUsing binornd with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how binornd changes the result.
Run a small binornd example, explain the result, then change one input and compare the output.
FAQ
Can n be fractional?⌄
No. Every trial count must be a positive integer.
How are array parameters combined?⌄
A scalar expands to the other parameter's shape. Two nonscalar parameters must have the same shape; binornd does not apply general singleton-dimension expansion.
Which class does binornd return?⌄
A single n or p selects single output. Otherwise the result is double. Size controls never select the class.
Can a call be reproduced?⌄
Yes. Call rng("default") before the operation to reset RunMat's shared deterministic random state.
Does binornd accept fixed-width integers?⌄
RunMat mode accepts exact fixed-width parameters and structural sizes. MATLAB compatibility mode restricts the call to documented single and double inputs.
What happens to a gpuArray input?⌄
RunMat samples through its shared host RNG and restores class and residency through the exact input owner. Explicit residency cannot be silently discarded.
Related Stats functions
Random
bootstrp · datasample · dividerand · exprnd · gamrnd · gpurng · lhsdesign · mvnrnd · normrnd · random · randsample · rng · trnd · unidrnd · unifrnd · wblrnd
Summary
anova1 · binocdf · binscatter · boxplot · cdf · cdfplot · chi2cdf · corr · corrcoef · corrcov · cov · cov2corr · dummyvar · ecdf · filloutliers · fitdist · geomean · grpstats · harmmean · icdf · isoutlier · ksdensity · kstest · kurtosis · lsline · mad · mode · nanmax · normalize · normcdf · norminv · normpdf · onehotdecode · onehotencode · pca · pdf · prctile · qqplot · quantile · refline · rmse · scatterhist · skewness · tabulate · tcdf · tiedrank · tinv · tpdf · ttest · ttest2 · wblinv
Ml
bayesopt · classify · confusionmat · crossvalind · cvpartition · fitclinear · fitctree · fitlm · kmeans · knnsearch · lasso · lassoglm · linkage · lscov · mnrfit · nlparci · optimizableVariable · pdist · pdist2 · perfcurve · predict · regress · ridge · squareform · test · training · tsne
Hist
Source you can read
Every RunMat function's source is public. Read exactly how binornd is executed, line by line, in Rust.
- View the source for binornd in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.