pxt-calliope/docs/reference/math/random-boolean.md
Juri Wolf 5f7a8e5301
Updates for V4 (#197)
* update yotta defaults for 16kb devices

* refactor deprecated blocks

* updates for button events

* update button events

* update refference

* update docs

* update docs

* update button event blocks

* update docs

* update block id
2022-08-10 09:36:19 -07:00

39 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# random Boolean
Returns a pseudo-random boolean value that is either `true` or `false`.
```sig
Math.randomBoolean()
```
## Returns
* a pseudo-random [boolean](types/boolean) that is either `true` or `false`.
### ~ hint
#### What is pseudo-random?
Random numbers generated on a computer are often called pseudo-random. This because the method to create the number is based on some starting value obtained from the computer itself. The formula for the random number could use some amount of mathematical operations on a value derived from a timer or some other input. The resulting "random" number isnt considered entirely random because it started with some initial value and a repeatable set of operations on it. Therefore, its called a pseudo-random number.
A random boolean is created by choosing a [random int](/reference/math/randint) ranging from `0` to `1`.
### ~
## Example
Make your @boardname@ do a coin toss when it's dropped softly. Have the LEDs show 'heads' or 'tails' as the result of the toss.
```blocks
input.onGesture(Gesture.FreeFall, () => {
if (Math.randomBoolean()) {
basic.showIcon(IconNames.Happy)
} else {
basic.showIcon(IconNames.Sword)
}
})
```
## See Also
[random int](/reference/math/randint)