Skip to content

Commit 1fd3f14

Browse files
Apply suggestions from code review
1 parent 4c3e747 commit 1fd3f14

File tree

8 files changed

+54
-45
lines changed

8 files changed

+54
-45
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"blurb": "Random number generation using Math.random()",
33
"authors": ["SneakyMallard"],
4-
"contributors":[]
4+
"contributors": ["SleeplessByte"]
55
}

concepts/randomness/about.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Introduction
22

3-
Many programs need (apparently) random values to simulate real-world events.
3+
Many programs need (pseudo-)random values to simulate real-world events.
44

55
Common, familiar examples include:
66

@@ -9,7 +9,25 @@ Common, familiar examples include:
99
- Shuffling a deck of cards: a random ordering of a card list.
1010
- The creation of trees and bushes in a 3-D graphics simulation.
1111

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.
24+
25+
[rfc]: https://www.w3.org/TR/webcrypto-2/
26+
[spec]: https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-math.random
27+
[crypto-web]: https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
28+
[crypto-node]: https://nodejs.org/api/webcrypto.html#cryptogetrandomvaluestypedarray
29+
[crypto-deno]: https://docs.deno.com/api/web/~/Crypto
30+
~~~
1331

1432
## Generating random numbers
1533

@@ -26,16 +44,18 @@ getRandomInRange(4, 10);
2644
// => 5.72
2745
```
2846

47+
<!-- prettier-ignore -->
48+
~~~exercism/advanced
49+
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.
52+
53+
[bias]: https://adammil.net/blog/v134_Efficiently_generating_random_numbers_without_bias.html
54+
~~~
2955
## Generating random integers
3056

3157
To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.
3258

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.
38-
```
3959

4060
[why-randomness-is-hard]: https://www.malwarebytes.com/blog/news/2013/09/in-computers-are-random-numbers-really-random
4161
[Math.random]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

concepts/randomness/introduction.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Introduction
22

3-
Many programs need (apparently) random values to simulate real-world events.
3+
Many programs need (pseudo-)random values to simulate real-world events.
44

55
Common, familiar examples include:
66

@@ -9,7 +9,13 @@ Common, familiar examples include:
99
- Shuffling a deck of cards: a random ordering of a card list.
1010
- The creation of trees and bushes in a 3-D graphics simulation.
1111

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+
~~~
1319

1420
## Generating random numbers
1521

@@ -30,12 +36,6 @@ getRandomInRange(4, 10);
3036

3137
To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.
3238

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.
38-
```
3939

4040
[why-randomness-is-hard]: https://www.malwarebytes.com/blog/news/2013/09/in-computers-are-random-numbers-really-random
4141
[Math.random]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

exercises/concept/captains-log/.docs/instructions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ When it rendezvous with another starship, Mary needs to log the registry number
1414

1515
Registry numbers start with the prefix "NCC-" and then use a number from 1000 to 9999 (both inclusive).
1616

17-
Implement the randomShipRegistryNumber() function that returns a random starship registry number.
17+
Implement the `randomShipRegistryNumber()` function that returns a random starship registry number.
1818

1919
```javascript
2020
randomShipRegistryNumber();
@@ -29,7 +29,7 @@ A stardate is a floating point number.
2929
The adventures of the Starship Enterprise from the first season of The Next Generation take place between the stardates 41000.0 and 42000.0.
3030
The "4" stands for the 24th century, the "1" for the first season.
3131

32-
Implement the function randomStardate that returns a floating point number between 41000.0 (inclusive) and 42000.0 (exclusive).
32+
Implement the function `randomStardate` that returns a floating point number between 41000.0 (inclusive) and 42000.0 (exclusive).
3333

3434
```javascript
3535
randomStardate();
@@ -43,7 +43,7 @@ Planets in the Star Trek universe are split into categories based on their prope
4343
For example, Earth is a class M planet.
4444
All possible planetary classes are: D, H, J, K, L, M, N, R, T, and Y.
4545

46-
Implement the randomPlanetClassfunction.
46+
Implement the `randomPlanetClass()` function.
4747
It should return one of the planetary classes at random.
4848

4949
```javascript
Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Introduction
22

3-
Many programs need (apparently) random values to simulate real-world events.
3+
Many programs need (pseudo-)random values to simulate real-world events.
44

55
Common, familiar examples include:
66

@@ -9,33 +9,20 @@ Common, familiar examples include:
99
- Shuffling a deck of cards: a random ordering of a card list.
1010
- The creation of trees and bushes in a 3-D graphics simulation.
1111

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+
~~~
1318

1419
## Generating random numbers
1520

1621
In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
1722
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).
1823

19-
To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:
2024

21-
```javascript
22-
function getRandomInRange(min, max) {
23-
return min + Math.random() * (max - min);
24-
}
25-
getRandomInRange(4, 10);
26-
// => 5.72
27-
```
2825

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.
38-
```
3926

4027
[why-randomness-is-hard]: https://www.malwarebytes.com/blog/news/2013/09/in-computers-are-random-numbers-really-random
4128
[Math.random]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

exercises/concept/captains-log/.meta/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"authors": [
33
"SneakyMallard"
44
],
5+
"contributors": [
6+
"SleeplessByte"
7+
],
58
"files": {
69
"solution": [
710
"captains-log.js"

exercises/concept/captains-log/.meta/exemplar.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/// <reference path="./global.d.ts" />
21
// @ts-check
32

43
/**
@@ -7,7 +6,7 @@
76
* @returns {string} the generated registry number.
87
*/
98
export function randomShipRegistryNumber() {
10-
return 'NCC-' + Math.floor(1000 + Math.random() * 9000);
9+
return `NCC-${Math.floor(1000 + Math.random() * 9000)}`;
1110
}
1211

1312
/**
@@ -19,12 +18,13 @@ export function randomStardate() {
1918
return 41000 + Math.random() * 1000;
2019
}
2120

21+
const PLANET_CLASSES = 'DHJKLMNRTY';
22+
2223
/**
2324
* Generates a random planet class.
2425
*
2526
* @returns {string} a one-letter planet class.
2627
*/
2728
export function randomPlanetClass() {
28-
const planetClasses = ['D', 'H', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'Y'];
29-
return planetClasses[Math.floor(Math.random() * 10)];
29+
return PLANET_CLASSES[Math.floor(Math.random() * PLANET_CLASSES.length)];
3030
}

exercises/concept/captains-log/captains-log.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/// <reference path="./global.d.ts" />
21
// @ts-check
32

43
/**

0 commit comments

Comments
 (0)