Skip to Content
ReferencesampleRandomNumbers

<sampleRandomNumbers/>

Samples random numbers from a distribution

<sampleRandomNumbers/> is a Math component that generates random numbers from a specified range of values, from a normal, log-normal, or mixture-of-normals distribution, or from one of the discrete count distributions (hypergeometric, binomial, and Poisson). It differs from the <selectRandomNumbers/> component in that the values can be altered within the document by resampling or changing one or more of the component’s attributes. The primary application of the <sampleRandomNumbers/> component is for use in simulations involving random numbers.

Attributes and Properties

Attributes for <sampleRandomNumbers>

Highlighted (3)
asList

boolean. Default value: true. Whether to render the items separated by commas (true) or with no separator (false).

number. Default value: 1. Number of samples to draw.

type

keyword. Distribution from which to sample.

ValueDescription
uniform (default)Continuous uniform distribution over [from, to].
discreteUniformAdded in 0.7.10Discrete uniform distribution over integers in [from, to].
gaussianNormal (Gaussian) distribution with the specified mean and standard deviation.
logNormalIn developmentLog-normal distribution: the exponential of a normal distribution with the specified logMean and logStandardDeviation.
normalMixtureIn developmentMixture of normal distributions: each value comes from one of the distributions described by means and standardDeviations, chosen with probability in proportion to its entry in weights.
hypergeometricAdded in 0.7.27Number of successes when drawing numDraws items without replacement from a population of numTotal items containing numSuccesses successes.
binomialAdded in 0.7.27Number of successes in numTrials independent trials that each succeed with the given probability.
poissonAdded in 0.7.27Poisson distribution with the specified mean (which defaults to 1).
Uniform and discrete-uniform parameters (4)

numberList. Values to exclude from the sample space.

number. Lower bound of the sampling range.

number. Default value: 1. Step size between samples for the discrete-uniform distribution.

number. Upper bound of the sampling range.

Gaussian and Poisson parameters (3)
mean

number. Mean of the sampling distribution (Gaussian or Poisson). Defaults to 0 for Gaussian and 1 for Poisson.

standardDeviation

number. Default value: 1. Standard deviation of the sampling distribution (Gaussian).

variance

number. Default value: 1. Variance of the sampling distribution (Gaussian).

Log-normal parameters (3)
logMeanIn development

number. Default value: 0. Mean of the underlying normal distribution (log-normal).

logStandardDeviationIn development

number. Default value: 1. Standard deviation of the underlying normal distribution (log-normal).

logVarianceIn development

number. Default value: 1. Variance of the underlying normal distribution (log-normal).

Normal mixture parameters (4)
meansIn development

numberList. Means of the normal distributions being mixed (normal mixture). How many are listed is how many components the mixture has.

standardDeviationsIn development

numberList. Default value: [1]. Standard deviations of the normal distributions being mixed (normal mixture). A single value applies to every component.

variancesIn development

numberList. Default value: [1]. Variances of the normal distributions being mixed (normal mixture). A single value applies to every component.

weightsIn development

numberList. Default value: [1]. Relative weight of each normal distribution being mixed (normal mixture), which need not add up to 1. A single value applies to every component, weighting them equally.

Hypergeometric and binomial parameters (5)
numDrawsAdded in 0.7.27

number. Number of items drawn without replacement to form each sample (hypergeometric).

numSuccessesAdded in 0.7.27

number. Number of successes in the population drawn from (hypergeometric).

numTotalAdded in 0.7.27

number. Size of the population drawn from (hypergeometric).

numTrialsAdded in 0.7.27

number. Default value: 1. Number of independent trials making up each sample (binomial).

probabilityAdded in 0.7.27

number. Default value: 0.5. Probability that each trial succeeds (binomial).

Number display (5)
avoidScientificNotationAdded in 0.7.16

boolean. Default value: false. Whether to render numbers in full decimal form rather than scientific notation.

displayDecimals

integer. Default value: 2. Number of decimal places to display when rendering this number.

displayDigits

integer. Default value: 3. Number of significant digits to display when rendering this number.

displaySmallAsZero

number. Default value: 1e-14. Threshold below which numbers are displayed as zero.

padZeros

boolean. Default value: false. Whether to pad displayed numbers with trailing zeros to fill the requested digits/decimals.

Other (1)
variantDeterminesSeed

boolean. Whether the document's variant index determines the random seed.

Common to all components (9)
copy

reference. Create an independent copy of another component by reference. Enter a references a $name.

disabled

boolean. Default value: false. Whether this component is disabled and cannot be interacted with.

extend

reference. Extend another component by reference, inheriting its children and attributes. Enter a reference as $name.

fixed

boolean. Default value: false. Whether this component's value is fixed and cannot be modified.

fixLocation

boolean. Default value: false. Whether this component's location is fixed (preventing it from being moved while still allowing other modifications).

hide

boolean. Default value: false. Whether to hide this component from the rendered output.

isResponse

boolean. Default value: false. Whether this component is treated as a response for the purposes of assessment.

name

text. The name used to reference this component from elsewhere in the document.

styleNumber

integer. Default value: 1. The style number used to select this component's visual styling from the available style definitions.

Properties for <sampleRandomNumbers name="s">

Highlighted (5)
$s.mean

number. Mean of the sampling distribution.

$s.numSamples

number. Number of samples to draw.

$s.standardDeviation

number. Standard deviation of the sampling distribution.

$s.type

text. Distribution from which to sample.

$s.variance

number. Variance of the sampling distribution.

Uniform and discrete-uniform parameters (3)
$s.from

number. Lower bound of the sampling range.

$s.step

number. Step size between sample values (for discrete distributions).

$s.to

number. Upper bound of the sampling range.

Log-normal parameters (3)
$s.logMeanIn development

number. Mean of the underlying normal distribution (log-normal).

$s.logStandardDeviationIn development

number. Standard deviation of the underlying normal distribution (log-normal).

$s.logVarianceIn development

number. Variance of the underlying normal distribution (log-normal).

Normal mixture parameters (4)
$s.meansIn development

numberList. Means of the normal distributions being mixed (normal mixture).

$s.standardDeviationsIn development

numberList. Standard deviations of the normal distributions being mixed (normal mixture).

$s.variancesIn development

numberList. Variances of the normal distributions being mixed (normal mixture).

$s.weightsIn development

numberList. Relative weight of each normal distribution being mixed (normal mixture), as written rather than scaled to add up to 1.

Hypergeometric and binomial parameters (5)
$s.numDrawsAdded in 0.7.27

number. Number of items drawn without replacement to form each sample (hypergeometric).

$s.numSuccessesAdded in 0.7.27

number. Number of successes in the population drawn from (hypergeometric).

$s.numTotalAdded in 0.7.27

number. Size of the population drawn from (hypergeometric).

$s.numTrialsAdded in 0.7.27

number. Number of independent trials making up each sample (binomial).

$s.probabilityAdded in 0.7.27

number. Probability that each trial succeeds (binomial).

Distribution moments (3)
$s.mean

number. Mean of the sampling distribution.

$s.standardDeviation

number. Standard deviation of the sampling distribution.

$s.variance

number. Variance of the sampling distribution.

Number display (5)
$s.avoidScientificNotationIn development

boolean. Whether to render numbers in full decimal form rather than scientific notation.

$s.displayDecimalsIn development

integer. Number of decimal places to display when rendering this number.

$s.displayDigitsIn development

integer. Number of significant digits to display when rendering this number.

$s.displaySmallAsZeroIn development

number. Threshold below which numbers are displayed as zero.

$s.padZerosIn development

boolean. Whether to pad displayed numbers with trailing zeros to fill the requested digits/decimals.

Other (1)
$s.variantDeterminesSeed

boolean. Whether the document's variant index determines the random seed.

Common to all components (8)
$s.disabledIn development

boolean. Whether this component is disabled and cannot be interacted with.

$s.doenetML

text. The DoenetML source code that produced this component.

$s.fixedIn development

boolean. Whether this component's value is fixed and cannot be modified.

$s.fixLocationIn development

boolean. Whether this component's location is fixed (preventing it from being moved while still allowing other modifications).

$s.hiddenIn development

boolean. Whether this component is hidden from the rendered output.

$s.hide

boolean. Whether to hide this component from the rendered output.

$s.isResponse

boolean. Whether this component is treated as a response for the purposes of assessment.

$s.styleNumber

integer. The style number used to select this component's visual styling from the available style definitions.

Examples

Example: Default <sampleRandomNumbers/>

Without any attributes specified, the default sampling type is uniform, and the default interval is [0,1)[0, 1). In this example, there are two ways to view different samples: selecting another variant, or refreshing the page.


Example: Using a re-sampling button

Without any attributes specified, the default sampling type is uniform, and the default interval is [0,1)[0, 1). The <callAction> button can be used to resample the value.

Attribute Examples

Attribute Example: numSamples

The <sampleRandomNumbers/> component is used with the numSamples attribute to select multiple random values in the default interval [0,1)[0,1). The default type is uniform.


Attribute Example: from, to

The <sampleRandomNumbers/> component is used with the from and the to attributes to modify the default selection interval. The default type is uniform.


Attribute Example: type = “discreteUniform”

The type attribute of the <sampleRandomNumbers/> component is specified as discreteUniform. The discreteUniform selection generates values from an evenly spaced range defined by the attributes to, from, and step. If left unspecified, the default step is 1.


Attribute Example: exclude

The type attribute of the <sampleRandomNumbers/> component is specified as discreteUniform. Values can be excluded from the specified range used with this type using the exclude attribute.


Attribute Example: step

The type attribute of the <sampleRandomNumbers/> component is specified as discreteUniform. The discreteUniform selection generates values from an evenly spaced range defined by the attributes to, from, and step. If left unspecified, the default step is 1.


Attribute Example: type = “gaussian”

The type attribute of the <sampleRandomNumbers/> component is specified as gaussian. The gaussian selection generates standard normal variables (mean 0, variance 1). Alternatively, the mean as well as the variance or the standardDeviation attributes may be defined.


Attribute Example: gaussian with mean and variance

The type attribute of the <sampleRandomNumbers/> component is specified as gaussian. The gaussian selection generates standard normal variables (mean 0, variance 1). In this example, the mean and the variance are defined with attributes.


Attribute Example: gaussian with mean and standardDeviation

The type attribute of the <sampleRandomNumbers/> component is specified as gaussian. The gaussian selection generates standard normal variables (mean 0, variance 1). In this example, the mean and the standardDeviation are defined with attributes.


Attribute Example: type = “logNormal”

The logNormal type raises ee to the power of a normal variable, so every sample is positive and the distribution is skewed to the right — the usual shape for quantities such as incomes, particle sizes, or times to finish a task.

Its parameters describe the normal distribution being exponentiated, not the samples themselves, which is why they are named logMean and logStandardDeviation rather than mean and standardDeviation. Either logStandardDeviation or logVariance may be given, as with the gaussian type. If unspecified, logMean is 0 and logStandardDeviation is 1.

Writing μ\mu for logMean and σ\sigma for logStandardDeviation, the reported mean is eμ+σ2/2e^{\mu + \sigma^2/2} and the reported variance is (eσ2−1) e2μ+σ2(e^{\sigma^2} - 1)\,e^{2\mu + \sigma^2}. Both grow very fast with σ\sigma, so a large one reports Infinity for a distribution whose true moments are beyond what a number can hold, and samples past about e709e^{709} come back as Infinity for the same reason. At the other end, a sample below about e−745e^{-745} rounds to 0, which is the only way a value can fail to be strictly positive.


Attribute Example: type = “normalMixture”

The normalMixture type draws each value from one of several normal distributions: a component is chosen at random, and the value comes from that component alone. It is the usual way to describe a population made of distinct groups — two study sections, two species, a process that sometimes runs slowly — and the usual way to produce a bimodal sample, which no single gaussian can.

The components are described by three lists. means gives each component’s center, and how many are listed is how many components there are — it is the one of the three with no default. standardDeviations (or variances, as with the gaussian type) gives each component’s spread, and weights gives each component’s share of the values. Weights are relative and need not add up to 1: weights="1 2" above means a third of the values come from the first component and two thirds from the second. A list holding a single value applies to every component, which is how the defaults — a spread of 1, and equal weights — are written, so standardDeviations="2" beside three means gives all three components a spread of 2.

The reported mean is the weighted average of the component means. The reported variance is the weighted average of the component variances plus a second term for how far the components sit from one another. That second term is zero exactly when every component carrying weight shares one center — a component whose weights entry is 0 contributes to neither term — and is positive otherwise. Writing pip_i, μi\mu_i and σi\sigma_i for the proportion, mean and standard deviation of each component, it is ∑ipiσi2+∑ipi(μi−μ)2\sum_i p_i \sigma_i^2 + \sum_i p_i (\mu_i - \mu)^2, where μ\mu is the reported mean.

Whether the values actually come out looking bimodal depends on the components. Two equally weighted components of equal spread are visibly two humps only once their centers are more than 2σ2\sigma apart; closer together than that, the mixture has a single peak. The type is named for what it does rather than for that outcome, and the separation is worth exploring in its own right.


Attribute Example: type = “hypergeometric”

The hypergeometric type counts the successes obtained when drawing numDraws items without replacement from a population of numTotal items containing numSuccesses successes. Because the draws within a single sample deplete the population, this is the without-replacement counterpart of the binomial type.

Each sample lies between max⁡(0,n−(N−K))\max(0, n - (N - K)) and min⁡(n,K)\min(n, K), where NN is numTotal, KK is numSuccesses, and nn is numDraws.

Unlike most of the distributions, hypergeometric has no defaults at all to fall back on. If any of the three attributes is omitted, or they do not describe a population (for example, more successes or more draws than there are items), every sample is NaN, as are the reported mean, variance, and standardDeviation.


Attribute Example: type = “binomial”

The binomial type counts the successes in numTrials independent trials that each succeed with the given probability. If unspecified, numTrials is 1 and probability is 0.5, so type="binomial" on its own produces a fair coin flip reported as 0 or 1.


Attribute Example: type = “poisson”

The poisson type is determined entirely by its mean, which also equals its variance. Unlike the gaussian type, whose mean defaults to 0, the mean of a poisson distribution defaults to 1, since a Poisson distribution with mean 0 always returns 0.

Limits on the discrete distributions

The three discrete distributions — hypergeometric, binomial, and poisson — draw their samples one item, trial, or event at a time, so very large parameters would take a long time and leave the page unresponsive while they ran. Parameters that would need more than ten million draws for a single sample are therefore treated as unusable, giving NaN samples and statistics along with a warning explaining what to change. The limit is far above the populations, trial counts, and rates that arise in practice; it is there so that mistyping an extra digit reports a problem instead of freezing the activity. An order of magnitude below that, from a million draws per sample, the samples are still drawn as asked, but a warning notes that sampling may be slow. Note that both figures apply to each sample individually — a large numSamples still takes proportionally longer.

Last updated on