Skip to content

Commit

Permalink
Add notes on inclusive vs exclusive functions for random
Browse files Browse the repository at this point in the history
  • Loading branch information
LaCuneta committed Jan 24, 2024
1 parent 6091860 commit 3fbb07e
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions app/static/md/random.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ ask patches [

Things to keep in mind when using `random`:

* Always remember that `random` will give us numbers within the range from 0 to N - 1, instead of 1 to N. For example, if we run `random 3`, we may get 0, 1, or 2, *but not 3*.
* If you want to generate a random number from `minnumber` to `maxnumber - 1`, you can use the following format: `minnumber + (random (maxnumber - minnumber))`. For example, if we wanted to generate a random number between 4 and 6 (that is, we want to get 4, 5, or 6), we could write the following code: `4 + random 3`.
* Always remember that `random` will give us numbers within the range from 0 to N - 1, instead of 1 to N. For example, if we run `random 3`, we may get 0, 1, or 2, *but not 3*.
* If you want to generate a random number from `A` to `B`, you can use the following format: `A + (random (B - A + 1))`. For example, if we wanted to generate a random number between 4 and 6 (that is, we want to get 4, 5, or 6), we could write the following code: `4 + random (6 - 4 + 1)` or just `4 + random 3`.
* `random` only generates positive integer numbers. If you need to generate floating point numbers, you should use `random-float`.
* The algorithm of `random` generates a *uniform distribution*. For example, every time we run `random 5`, there is an equal likelihood of getting 0, 1, 2, 3, or 4. If you would like to generate random numbers over a *normal distribution*, you should use `random-normal`.
* The algorithm of `random` generates a *uniform distribution*. For example, every time we run `random 5`, there is an equal likelihood of getting 0, 1, 2, 3, or 4. If you would like to generate random numbers over a *normal distribution*, you should use `random-normal`.

In the model example below, we have 6 turtles that represent the 6 faces of a dice. Every time we click the **roll dice** button, each turtle picks a random number between 1 to 6 (including 6) and updates its shape accordingly.


In the model example below, we have 6 turtles that represent the 6 faces of a dice. Every time we click the **roll dice** button, each turtle picks a random number between 1 to 6 (including 6) and updates its shape accordingly.

Why do we need to add 1 to value we give to random to get the full range of values? In math and computer science language, we would say that the `random` primitive produces numbers in the range 0 to N *exclusive*, meaning that the number N itself is *excluded* from the possible set of values. In order to make it *inclusive* for N we have tell `random` to set the *exclusive* limit at N + 1, which gets N back in the range of possible results.

0 comments on commit 3fbb07e

Please sign in to comment.