Skip to content

Commit 0539714

Browse files
quintuple-mallardSleeplessBytekotp
authored
Add Concept Exercise for 'randomness': Captain's Log (#2683)
* Create captains-log --------- Co-authored-by: Derk-Jan Karrenbeld <[email protected]> Co-authored-by: Victor Goff <[email protected]>
1 parent 5eeee38 commit 0539714

File tree

20 files changed

+546
-0
lines changed

20 files changed

+546
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"blurb": "Random number generation using Math.random()",
3+
"authors": ["SneakyMallard"],
4+
"contributors": ["SleeplessByte"]
5+
}

concepts/randomness/about.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Introduction
2+
3+
Many programs need (pseudo-)random values to simulate real-world events.
4+
5+
Common, familiar examples include:
6+
7+
- A coin toss: a random value from ('H', 'T').
8+
- The roll of a die: a random integer from 1 to 6.
9+
- Shuffling a deck of cards: a random ordering of a card list.
10+
- The creation of trees and bushes in a 3-D graphics simulation.
11+
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+
~~~
31+
32+
## Generating random numbers
33+
34+
In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
35+
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).
36+
37+
To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:
38+
39+
```javascript
40+
function getRandomInRange(min, max) {
41+
return min + Math.random() * (max - min);
42+
}
43+
getRandomInRange(4, 10);
44+
// => 5.72
45+
```
46+
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+
~~~
55+
56+
## Generating random integers
57+
58+
To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.
59+
60+
[why-randomness-is-hard]: https://www.malwarebytes.com/blog/news/2013/09/in-computers-are-random-numbers-really-random
61+
[Math.random]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Introduction
2+
3+
Many programs need (pseudo-)random values to simulate real-world events.
4+
5+
Common, familiar examples include:
6+
7+
- A coin toss: a random value from ('H', 'T').
8+
- The roll of a die: a random integer from 1 to 6.
9+
- Shuffling a deck of cards: a random ordering of a card list.
10+
- The creation of trees and bushes in a 3-D graphics simulation.
11+
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+
~~~
19+
20+
## Generating random numbers
21+
22+
In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
23+
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).
24+
25+
To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:
26+
27+
```javascript
28+
function getRandomInRange(min, max) {
29+
return min + Math.random() * (max - min);
30+
}
31+
getRandomInRange(4, 10);
32+
// => 5.72
33+
```
34+
35+
## Generating random integers
36+
37+
To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.
38+
39+
[why-randomness-is-hard]: https://www.malwarebytes.com/blog/news/2013/09/in-computers-are-random-numbers-really-random
40+
[Math.random]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

concepts/randomness/links.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random",
4+
"description": "MDN: The Math.random() function"
5+
}
6+
]

config.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,18 @@
422422
"type-conversion"
423423
],
424424
"status": "beta"
425+
},
426+
{
427+
"slug": "captains-log",
428+
"name": "Captain's Log",
429+
"uuid": "65cf28ab-243c-41cb-a720-f324f2cabe28",
430+
"concepts": [
431+
"randomness"
432+
],
433+
"prerequisites": [
434+
"numbers",
435+
"arithmetic-operators"
436+
]
425437
}
426438
],
427439
"practice": [
@@ -2851,6 +2863,11 @@
28512863
"uuid": "4e68e39a-e36c-4d2d-8714-eb6482e31ff5",
28522864
"slug": "while-loops",
28532865
"name": "While Loops"
2866+
},
2867+
{
2868+
"uuid": "ca322d6f-0f7e-4a2d-a058-e98a59cdae93",
2869+
"slug": "randomness",
2870+
"name": "Randomness"
28542871
}
28552872
],
28562873
"key_features": [
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Hints
2+
3+
## 1. Generate a random starship registry number
4+
5+
- To generate a random number in the range _min_ (inclusive) to _max_ (exclusive) you can use the snippet `min + Math.random()*(max - min)`.
6+
- There is a [built in function][floor] for turning a floating point number into an integer.
7+
8+
## 2.Generate a random stardate
9+
10+
- To generate a random number in the range _min_ (inclusive) to _max_ (exclusive) you can use the snippet `min + Math.random()*(max - min)`.
11+
12+
## 3. Generate a random planet
13+
14+
- You can use a randomly generated integer as an array index.
15+
16+
[floor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Instructions
2+
3+
Mary is a big fan of the TV series Star Trek: The Next Generation.
4+
She often plays pen-and-paper role playing games, where she and her friends pretend to be the crew of the Starship Enterprise.
5+
Mary's character is Captain Picard, which means she has to keep the captain's log.
6+
She loves the creative part of the game, but doesn't like to generate random data on the spot.
7+
8+
Help Mary by creating random generators for data commonly appearing in the captain's log.
9+
10+
### 1. Generate a random starship registry number
11+
12+
Enterprise (registry number NCC-1701) is not the only starship flying around!
13+
When it rendezvous with another starship, Mary needs to log the registry number of that starship.
14+
15+
Registry numbers start with the prefix "NCC-" and then use a number from 1000 to 9999 (both inclusive).
16+
17+
Implement the `randomShipRegistryNumber()` function that returns a random starship registry number.
18+
19+
```javascript
20+
randomShipRegistryNumber();
21+
// => "NCC-1947"
22+
```
23+
24+
### 2. Generate a random stardate
25+
26+
What's the use of a log if it doesn't include dates?
27+
28+
A stardate is a floating point number.
29+
The adventures of the Starship Enterprise from the first season of The Next Generation take place between the stardates 41000.0 and 42000.0.
30+
The "4" stands for the 24th century, the "1" for the first season.
31+
32+
Implement the function `randomStardate` that returns a floating point number between 41000.0 (inclusive) and 42000.0 (exclusive).
33+
34+
```javascript
35+
randomStardate();
36+
// => 41458.15721310934
37+
```
38+
39+
### 3. Generate a random planet
40+
41+
The Starship Enterprise encounters many planets in its travels.
42+
Planets in the Star Trek universe are split into categories based on their properties.
43+
For example, Earth is a class M planet.
44+
All possible planetary classes are: D, H, J, K, L, M, N, R, T, and Y.
45+
46+
Implement the `randomPlanetClass()` function.
47+
It should return one of the planetary classes at random.
48+
49+
```javascript
50+
randomPlanetClass();
51+
// => "K"
52+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Introduction
2+
3+
Many programs need (pseudo-)random values to simulate real-world events.
4+
5+
Common, familiar examples include:
6+
7+
- A coin toss: a random value from ('H', 'T').
8+
- The roll of a die: a random integer from 1 to 6.
9+
- Shuffling a deck of cards: a random ordering of a card list.
10+
- The creation of trees and bushes in a 3-D graphics simulation.
11+
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+
~~~
19+
20+
## Generating random numbers
21+
22+
In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
23+
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).
24+
25+
[why-randomness-is-hard]: https://www.malwarebytes.com/blog/news/2013/09/in-computers-are-random-numbers-really-random
26+
[Math.random]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
/bin/configlet
3+
/bin/configlet.exe
4+
/package-lock.json
5+
/yarn.lock
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": [
3+
"SneakyMallard"
4+
],
5+
"contributors": [
6+
"SleeplessByte"
7+
],
8+
"files": {
9+
"solution": [
10+
"captains-log.js"
11+
],
12+
"test": [
13+
"captains-log.spec.js"
14+
],
15+
"exemplar": [
16+
".meta/exemplar.js"
17+
]
18+
},
19+
"blurb": "Learn about randomness and the Math.random() function while helping Mary generate stardates and starship registry numbers for her Star Trek roleplay."
20+
}

0 commit comments

Comments
 (0)