You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Shuffling a deck of cards: a random ordering of a card list.
10
10
- The creation of trees and bushes in a 3-D graphics simulation.
11
11
12
-
Generating truly random values with a computer is a [surprisingly difficult technical challenge][why-randomness-is-hard], so you may see these results referred to as "pseudorandom".
12
+
Generating truly random values with a computer is a [surprisingly difficult technical challenge][why-randomness-is-hard], which is why there are also "pseudorandom" generators.
13
+
14
+
<!-- prettier-ignore -->
15
+
~~~exercism/advanced
16
+
[The language specification][spec] for JavaScript doesn't force the implementation for random number generation.
17
+
All major browsers and JavaScript runtimes implement a PRNG (pseudo-random number generator).
18
+
Because the numbers are not cryptographically secure, they should never be used for anything that requires true or at least cryptographically secure random numbers, such as certificate or password generation or operations.
19
+
20
+
There is a standard called [Web Cryptography][rfc] which standardizes an interface for doing cryptography in JavaScript.
21
+
It is implemented [by Browsers][crypto-web] as well as runtimes such as [Node.JS][crypto-node] and [Deno][crypto-deno].
22
+
23
+
This concept is not about Web Crypto and will restrict itself to pseudo-random number generation.
Most simple techniques of returning a range of numbers based on the randomly generated number [will introduce bias][bias].
50
+
That means that some numbers will be more likely to be rolled than others.
51
+
Using the multiplication technique spreads out the bias over the entire range, so it will be less obvious and in most cases not a big issue, but you should be aware of this.
- Shuffling a deck of cards: a random ordering of a card list.
10
10
- The creation of trees and bushes in a 3-D graphics simulation.
11
11
12
-
Generating truly random values with a computer is a [surprisingly difficult technical challenge][why-randomness-is-hard], so you may see these results referred to as "pseudorandom".
12
+
Generating truly random values with a computer is a [surprisingly difficult technical challenge][why-randomness-is-hard], which is why there are also "pseudorandom" generators.
13
+
14
+
<!-- prettier-ignore -->
15
+
~~~exercism/caution
16
+
The `Math.random()` function should NOT be used for security and cryptographic applications!
17
+
Finish the learning exercise(s) about this concept to learn more
18
+
~~~
13
19
14
20
## Generating random numbers
15
21
@@ -30,12 +36,6 @@ getRandomInRange(4, 10);
30
36
31
37
To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.
32
38
33
-
```exercism/caution
34
-
35
-
The `Math.random()` function should NOT be used for security and cryptographic applications!!
36
-
37
-
Instead, you can use the Web Crypto API, which provides various cryptographic functions.
- Shuffling a deck of cards: a random ordering of a card list.
10
10
- The creation of trees and bushes in a 3-D graphics simulation.
11
11
12
-
Generating truly random values with a computer is a [surprisingly difficult technical challenge][why-randomness-is-hard], so you may see these results referred to as "pseudorandom".
12
+
Generating truly random values with a computer is a [surprisingly difficult technical challenge][why-randomness-is-hard], which is why there are also "pseudorandom" generators.
13
+
<!-- prettier-ignore -->
14
+
~~~exercism/caution
15
+
The `Math.random()` function should NOT be used for security and cryptographic applications!
16
+
Finish the learning exercise(s) about this concept to learn more
17
+
~~~
13
18
14
19
## Generating random numbers
15
20
16
21
In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
17
22
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).
18
23
19
-
To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:
20
24
21
-
```javascript
22
-
functiongetRandomInRange(min, max) {
23
-
return min +Math.random() * (max - min);
24
-
}
25
-
getRandomInRange(4, 10);
26
-
// => 5.72
27
-
```
28
25
29
-
## Generating random integers
30
-
31
-
To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.
32
-
33
-
```exercism/caution
34
-
35
-
The `Math.random()` function should NOT be used for security and cryptographic applications!!
36
-
37
-
Instead, you can use the Web Crypto API, which provides various cryptographic functions.
0 commit comments