Thursday, June 13, 2019

Using the random function in Desmos

Desmos has recently added a new function to its graphing calculator for generating random numbers.

Basic usage

The simplest way to use it is to type random() to get a random number between 0 and 1.



You can get the calculator to generate new random numbers by using the crossed arrows next to the plus.




Rolling a die

What if you don't want a random number between 0 and 1? For instance, perhaps you want to simulate a dice roll.

In the olden days you might do something like floor(6*random() + 1) and this also works in Desmos. However, we can also use Desmos to select a random element of a list. So, this is a more elegant solution:


Or, if you don't want to type all the numbers on a die, you could even use this:





Drawing from a distribution

Desmos also has the ability to work with probability distributions, and the random function works on these too. (If you didn't know about the distribution functions you should stop reading now, and go and explore them as they are very impressive!)

For instance, if you know that IQs are normally distributed with a mean of 100 and a standard deviation of 15, you can set this up in Desmos and then ask for a random IQ from this distribution.



If you are concerned that IQs should be integers even though the underlying distribution is normal, you can treat it like any other expression and write round(I.random).

If you want to simulate rolling 100 dice and counting the number of 3s that are rolled, this could be accomplished using a binomial distribution.


Maybe you want the fun of using distributions but you want all outcomes to be equally likely? For instance, maybe you want to choose a random real number between 5 and 20. You could devote a few seconds to thinking and come up with an expression like 15random() + 5 but why would you do that when Desmos has uniform distributions?



Generating multiple random numbers

With all the methods shown so far, it is also possible to ask Desmos to generate multiple random numbers at once. For instance, perhaps you want to generate 100 random numbers between 0 and 1?

Simple enough, just use random(100).

Or if you want to simulate the rolling of 5 dice, you could use [1,...,6].random(5). Perhaps you want to note their total? That is straightforward.



You can also draw a number of random samples from some distribution. For instance, perhaps you want to generate a sample of 5000 random IQs based on the normal distribution described above. The results can then be plotted as a histogram.




Once the sample is shown, it is worth clicking on the graph of the distribution itself, to see how closely it matches the histogram. You can also generate another random sample using the crossed arrows up the top.

There are many more applications of the random function for helping to visualise distributions and sampling but hopefully this gives you some idea of the potential.




4 comments:

  1. Sir,
    Can you help me on my post at reddit for random number generation
    https://www.reddit.com/r/desmos/comments/gksa9m/desmos_random_velocities_required_at_different/

    ReplyDelete
  2. Very clear and very helpful. Thank you!

    ReplyDelete
  3. Can you seed Desmos like the TI 84 graphing calculator? As a teacher, I would like to produce random numbers as a class, but I want to do a simulation together and I want the class to all get the same results. I want to use the random number generator [1,...,5].random

    ReplyDelete
  4. Good question. I think the answer is no based on the very bottom of this page (https://support.desmos.com/hc/en-us/articles/360022401451-Statistics) which says that although you can pass a seed parameter in, that is only part of the seed that is actually used.

    ReplyDelete