Random

Generating Random Values

The random( ) function generates values that you can influence by adding a number, list, or distribution.

Expression list showing the random function with one, two, and n number of outputs based on given inputs. Screenshot. 

Entering random\(( )\) generates a single value sampled uniformly from the interval \([0,1)\).

Adding a number within the parentheses, like random\((2)\), displays that amount of random values.

Expression list showing the random function influenced by a three-value list. Random function outputs one, two and n number of outputs based on given inputs. Screenshot. 

To randomly sample from a list of numbers, enter list.random\(( )\).

For example, \([1,2,3]\).random\(( )\) returns a single value sampled uniformly from the list.

Expression list shows binomial distribution and then the random function influenced by that distribution. Random function outputs one and three values based on given inputs. Screenshot. 

To sample from a specific distribution, type distribution.random\(( )\).

For example, binomialdist\((10)\).random\(( )\) will return a single value randomly sampled from a binomial distribution with \(10\) trials.

 

Rerandomizing and Random Seeds

If any expression contains a random\(( )\) function, a Randomize icon will appear in the header of the expression list.

Behind the scenes, Desmos uses a pseudorandom number generator (PRNG) to create sequences of numbers that look random, even though they’re generated by a fixed process. The sequence is based on an initial value called a seed. Clicking Randomize sets the global seed to a new value, which rerandomizes all expressions that use random\(( )\).

If you’d like more control over a specific random\(( )\) function, you can add a seed value, such as random\((2,a)\). Changing the value of \(a\) will only rerandomize that one expression, rather than the entire expression list. This can be especially useful with sliders, since moving a slider can trigger new random values automatically, such as in an animation or simulation.

While adding a seed value can influence how randomness behaves, it doesn’t guarantee the same value each time.

Random function in the expression list with the Randomize icon highlighted in a callout box. Screenshot.

 

Change to Random Seeds in Function Definitions

Starting in March 2024, when random\(( )\) or shuffle\(( )\) appears inside a function definition, the seed now automatically includes the values of that function’s parameters. As a result, each time the function is applied to different inputs, it will generate different random values.

For example, \(f(x)=\) random\(( )\) previously produced the same value for every input, meaning \(f(1)\) and \(f(2)\) would always match. With the new behavior, \(f(1)\) and \(f(2)\) now produce different results, while repeated calls of \(f(1)\) still match.

This change makes randomness behave more intuitively. Different inputs lead to different random values, and identical inputs stay consistent and open the door to fun applications such as the Random Walk graph.

Graphs created before March 2024 still exhibit the previous behavior by default, but they will offer the option to update to the new behavior.