Skip to content

Commit ac1d8da

Browse files
Format 12 files
1 parent 0cdd9ac commit ac1d8da

File tree

12 files changed

+112
-76
lines changed

12 files changed

+112
-76
lines changed

concepts/randomness/about.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,38 @@ Many programs need (apparently) random values to simulate real-world events.
44

55
Common, familiar examples include:
66

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.
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.
1111

1212
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".
13+
1314
## Generating random numbers
15+
1416
In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
1517
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).
1618

17-
To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:
19+
To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:
20+
1821
```javascript
1922
function getRandomInRange(min, max) {
20-
return min + Math.random() * (max - min) ;
23+
return min + Math.random() * (max - min);
2124
}
22-
getRandomInRange(4, 10)
25+
getRandomInRange(4, 10);
2326
// => 5.72
2427
```
28+
2529
## Generating random integers
30+
2631
To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.
27-
~~~~exercism/caution
32+
33+
```exercism/caution
2834
2935
The `Math.random()` function should NOT be used for security and cryptographic applications!!
3036
3137
Instead, you can use the Web Crypto API, which provides various cryptographic functions.
32-
~~~~
38+
```
3339

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

concepts/randomness/introduction.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,38 @@ Many programs need (apparently) random values to simulate real-world events.
44

55
Common, familiar examples include:
66

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.
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.
1111

1212
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".
13+
1314
## Generating random numbers
15+
1416
In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
1517
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).
1618

17-
To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:
19+
To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:
20+
1821
```javascript
1922
function getRandomInRange(min, max) {
20-
return min + Math.random() * (max - min) ;
23+
return min + Math.random() * (max - min);
2124
}
22-
getRandomInRange(4, 10)
25+
getRandomInRange(4, 10);
2326
// => 5.72
2427
```
28+
2529
## Generating random integers
30+
2631
To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.
27-
~~~~exercism/caution
32+
33+
```exercism/caution
2834
2935
The `Math.random()` function should NOT be used for security and cryptographic applications!!
3036
3137
Instead, you can use the Web Crypto API, which provides various cryptographic functions.
32-
~~~~
38+
```
3339

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

concepts/randomness/links.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"url":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random",
3+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random",
44
"description": "MDN: The Math.random() function"
55
}
66
]

config.json

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,17 @@
424424
"status": "beta"
425425
},
426426
{
427-
"uuid": "65cf28ab-243c-41cb-a720-f324f2cabe28",
428-
"slug": "captains-log",
429-
"name": "Captain's Log",
430-
"concepts": ["randomness"],
431-
"prerequisites": ["numbers", "arithmetic-operators"]
432-
}
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+
]
437+
}
433438
],
434439
"practice": [
435440
{
@@ -2859,7 +2864,7 @@
28592864
"slug": "while-loops",
28602865
"name": "While Loops"
28612866
},
2862-
{
2867+
{
28632868
"uuid": "ca322d6f-0f7e-4a2d-a058-e98a59cdae93",
28642869
"slug": "randomness",
28652870
"name": "Randomness"
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# Hints
22

33
## 1. Generate a random starship registry number
4-
- To generate a random number in the range _min_ (inclusive) to _max_ (exclusive) you can use the snippet `min + Math.random()*(max - min)`.
5-
- There is a [built in function][floor] for turning a floating point number into an integer.
6-
## 2.Generate a random stardate
7-
- To generate a random number in the range _min_ (inclusive) to _max_ (exclusive) you can use the snippet `min + Math.random()*(max - min)`.
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+
812
## 3. Generate a random planet
13+
914
- You can use a randomly generated integer as an array index.
10-
15+
1116
[floor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
Mary is a big fan of the TV series Star Trek: The Next Generation. She often plays pen-and-paper role playing games, where she and her friends pretend to be the crew of the Starship Enterprise. Mary's character is Captain Picard, which means she has to keep the captain's log. She loves the creative part of the game, but doesn't like to generate random data on the spot.
44

55
Help Mary by creating random generators for data commonly appearing in the captain's log.
6+
67
### 1. Generate a random starship registry number
78

89
Enterprise (registry number NCC-1701) is not the only starship flying around! When it rendezvous with another starship, Mary needs to log the registry number of that starship.
910

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

1213
Implement the randomShipRegistryNumber() function that returns a random starship registry number.
14+
1315
```javascript
14-
randomShipRegistryNumber()
16+
randomShipRegistryNumber();
1517
// => "NCC-1947"
1618
```
19+
1720
### 2. Generate a random stardate
1821

1922
What's the use of a log if it doesn't include dates?
@@ -23,15 +26,17 @@ A stardate is a floating point number. The adventures of the Starship Enterprise
2326
Implement the function randomStardate that returns a floating point number between 41000.0 (inclusive) and 42000.0 (exclusive).
2427

2528
```javascript
26-
randomStardate()
29+
randomStardate();
2730
// => 41458.15721310934
2831
```
32+
2933
### 3. Generate a random planet
3034

3135
The Starship Enterprise encounters many planets in its travels. Planets in the Star Trek universe are split into categories based on their properties. For example, Earth is a class M planet. All possible planetary classes are: D, H, J, K, L, M, N, R, T, and Y.
3236

3337
Implement the randomPlanetClassfunction. It should return one of the planetary classes at random.
38+
3439
```javascript
35-
randomPlanetClass()
40+
randomPlanetClass();
3641
// => "K"
3742
```

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,38 @@ Many programs need (apparently) random values to simulate real-world events.
44

55
Common, familiar examples include:
66

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.
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.
1111

1212
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".
13+
1314
## Generating random numbers
15+
1416
In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
1517
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).
1618

17-
To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:
19+
To get a random number between _min_ (inclusive) and _max_ (exclusive) you can use a function something like this:
20+
1821
```javascript
1922
function getRandomInRange(min, max) {
20-
return min + Math.random() * (max - min) ;
23+
return min + Math.random() * (max - min);
2124
}
22-
getRandomInRange(4, 10)
25+
getRandomInRange(4, 10);
2326
// => 5.72
2427
```
28+
2529
## Generating random integers
30+
2631
To generate a random integer, you can use `Math.floor()` or `Math.ceil()` to turn a randomly generated number into an integer.
27-
~~~~exercism/caution
32+
33+
```exercism/caution
2834
2935
The `Math.random()` function should NOT be used for security and cryptographic applications!!
3036
3137
Instead, you can use the Web Crypto API, which provides various cryptographic functions.
32-
~~~~
38+
```
3339

3440
[why-randomness-is-hard]: https://www.malwarebytes.com/blog/news/2013/09/in-computers-are-random-numbers-really-random
3541
[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: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22
"authors": [
33
"SneakyMallard"
44
],
5-
"contributors": [],
6-
"forked-from": ["java/captains-log"],
7-
"concept": [
8-
{
9-
"uuid": "65cf28ab-243c-41cb-a720-f324f2cabe28",
10-
"slug": "captains-log",
11-
"name": "Captain's Log",
12-
"concepts": ["randomness"],
13-
"prerequisites": ["numbers", "arithmetic-operators"]
14-
}
15-
],
165
"files": {
17-
"solution": ["captains-log.js"],
18-
"test": ["captains-log.spec.js"],
19-
"exemplar": [".meta/exemplar.js"]
6+
"solution": [
7+
"captains-log.js"
8+
],
9+
"test": [
10+
"captains-log.spec.js"
11+
],
12+
"exemplar": [
13+
".meta/exemplar.js"
14+
]
2015
},
2116
"blurb": "Learn about randomness and the Math.random() function while helping Mary generate stardates and starship registry numbers for her Star Trek roleplay."
2217
}

exercises/concept/captains-log/.meta/design.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ The goal of this exercise is to teach the student how to generate psuedorandom n
1515
- Details of pseudorandom number generation in general.
1616
- Different algorithms for pseudorandom number generation.
1717

18-
1918
## Concepts
2019

2120
The Concepts this exercise unlocks are:

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @returns {string} the generated registry number.
88
*/
99
export function randomShipRegistryNumber() {
10-
return "NCC-"+Math.floor(1000 + Math.random()*9000)
10+
return 'NCC-' + Math.floor(1000 + Math.random() * 9000);
1111
}
1212

1313
/**
@@ -16,7 +16,7 @@ export function randomShipRegistryNumber() {
1616
* @returns {number} a stardate between 41000 (inclusive) and 42000 (exclusive).
1717
*/
1818
export function randomStardate() {
19-
return 41000 + Math.random() * 1000
19+
return 41000 + Math.random() * 1000;
2020
}
2121

2222
/**
@@ -25,6 +25,6 @@ export function randomStardate() {
2525
* @returns {string} a one-letter planet class.
2626
*/
2727
export function randomPlanetClass() {
28-
const planetClasses = ['D', 'H', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'Y']
29-
return planetClasses[Math.floor(Math.random() * 10)]
28+
const planetClasses = ['D', 'H', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'Y'];
29+
return planetClasses[Math.floor(Math.random() * 10)];
3030
}

0 commit comments

Comments
 (0)