mirror of
https://github.com/jbranchaud/til
synced 2026-01-10 10:38:01 +00:00
Add Generate Random Integers as a javascript til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||||
|
|
||||||
_841 TILs and counting..._
|
_842 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -290,6 +290,7 @@ _841 TILs and counting..._
|
|||||||
- [Fill An Input With A Ton Of Text](javascript/fill-an-input-with-a-ton-of-text.md)
|
- [Fill An Input With A Ton Of Text](javascript/fill-an-input-with-a-ton-of-text.md)
|
||||||
- [for...in Iterates Over Object Properties](javascript/for-in-iterates-over-object-properties.md)
|
- [for...in Iterates Over Object Properties](javascript/for-in-iterates-over-object-properties.md)
|
||||||
- [Freeze An Object, Sorta](javascript/freeze-an-object-sorta.md)
|
- [Freeze An Object, Sorta](javascript/freeze-an-object-sorta.md)
|
||||||
|
- [Generate Random Integers](javascript/generate-random-integers.md)
|
||||||
- [Get The Location And Size Of An Element](javascript/get-the-location-and-size-of-an-element.md)
|
- [Get The Location And Size Of An Element](javascript/get-the-location-and-size-of-an-element.md)
|
||||||
- [Get The Time Zone Of The Client Computer](javascript/get-the-time-zone-of-the-client-computer.md)
|
- [Get The Time Zone Of The Client Computer](javascript/get-the-time-zone-of-the-client-computer.md)
|
||||||
- [Globally Install A Package With Yarn](javascript/globally-install-a-package-with-yarn.md)
|
- [Globally Install A Package With Yarn](javascript/globally-install-a-package-with-yarn.md)
|
||||||
|
|||||||
30
javascript/generate-random-integers.md
Normal file
30
javascript/generate-random-integers.md
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# Generate Random Integers
|
||||||
|
|
||||||
|
JavaScript's `Math` module has a built-in `random` function.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
> Math.random();
|
||||||
|
0.4663311937101857
|
||||||
|
```
|
||||||
|
|
||||||
|
It can be used to generate random floats between 0 and 1.
|
||||||
|
|
||||||
|
If you want integers, though, you're going to have to build your own function.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
function getRandomInt(max) {
|
||||||
|
return Math.floor(Math.random() * Math.floor(max));
|
||||||
|
}
|
||||||
|
|
||||||
|
> getRandomInt(10);
|
||||||
|
1
|
||||||
|
> getRandomInt(10);
|
||||||
|
7
|
||||||
|
> getRandomInt(10);
|
||||||
|
2
|
||||||
|
```
|
||||||
|
|
||||||
|
This function allows you to randomly generate numbers between `0` (inclusive)
|
||||||
|
and `max`, a number you specify (exclusive).
|
||||||
|
|
||||||
|
[source](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)
|
||||||
Reference in New Issue
Block a user