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
Copy file name to clipboardExpand all lines: concepts/randomness/about.md
+15-9Lines changed: 15 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,32 +4,38 @@ Many programs need (apparently) random values to simulate real-world events.
4
4
5
5
Common, familiar examples include:
6
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.
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
11
12
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".
13
+
13
14
## Generating random numbers
15
+
14
16
In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
15
17
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).
16
18
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
+
18
21
```javascript
19
22
functiongetRandomInRange(min, max) {
20
-
return min +Math.random() * (max - min);
23
+
return min +Math.random() * (max - min);
21
24
}
22
-
getRandomInRange(4, 10)
25
+
getRandomInRange(4, 10);
23
26
// => 5.72
24
27
```
28
+
25
29
## Generating random integers
30
+
26
31
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
28
34
29
35
The `Math.random()` function should NOT be used for security and cryptographic applications!!
30
36
31
37
Instead, you can use the Web Crypto API, which provides various cryptographic functions.
Copy file name to clipboardExpand all lines: concepts/randomness/introduction.md
+15-9Lines changed: 15 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,32 +4,38 @@ Many programs need (apparently) random values to simulate real-world events.
4
4
5
5
Common, familiar examples include:
6
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.
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
11
12
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".
13
+
13
14
## Generating random numbers
15
+
14
16
In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
15
17
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).
16
18
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
+
18
21
```javascript
19
22
functiongetRandomInRange(min, max) {
20
-
return min +Math.random() * (max - min);
23
+
return min +Math.random() * (max - min);
21
24
}
22
-
getRandomInRange(4, 10)
25
+
getRandomInRange(4, 10);
23
26
// => 5.72
24
27
```
28
+
25
29
## Generating random integers
30
+
26
31
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
28
34
29
35
The `Math.random()` function should NOT be used for security and cryptographic applications!!
30
36
31
37
Instead, you can use the Web Crypto API, which provides various cryptographic functions.
Copy file name to clipboardExpand all lines: exercises/concept/captains-log/.docs/instructions.md
+8-3Lines changed: 8 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,17 +3,20 @@
3
3
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.
4
4
5
5
Help Mary by creating random generators for data commonly appearing in the captain's log.
6
+
6
7
### 1. Generate a random starship registry number
7
8
8
9
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.
9
10
10
11
Registry numbers start with the prefix "NCC-" and then use a number from 1000 to 9999 (both inclusive).
11
12
12
13
Implement the randomShipRegistryNumber() function that returns a random starship registry number.
14
+
13
15
```javascript
14
-
randomShipRegistryNumber()
16
+
randomShipRegistryNumber();
15
17
// => "NCC-1947"
16
18
```
19
+
17
20
### 2. Generate a random stardate
18
21
19
22
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
23
26
Implement the function randomStardate that returns a floating point number between 41000.0 (inclusive) and 42000.0 (exclusive).
24
27
25
28
```javascript
26
-
randomStardate()
29
+
randomStardate();
27
30
// => 41458.15721310934
28
31
```
32
+
29
33
### 3. Generate a random planet
30
34
31
35
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.
32
36
33
37
Implement the randomPlanetClassfunction. It should return one of the planetary classes at random.
Copy file name to clipboardExpand all lines: exercises/concept/captains-log/.docs/introduction.md
+15-9Lines changed: 15 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,32 +4,38 @@ Many programs need (apparently) random values to simulate real-world events.
4
4
5
5
Common, familiar examples include:
6
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.
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
11
12
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".
13
+
13
14
## Generating random numbers
15
+
14
16
In Javascript, you can generate psuedorandom numbers using the [`Math.random()`][Math.random] function.
15
17
It will return a psuedorandom floating-point number between 0 (inclusive), and 1 (exclusive).
16
18
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
+
18
21
```javascript
19
22
functiongetRandomInRange(min, max) {
20
-
return min +Math.random() * (max - min);
23
+
return min +Math.random() * (max - min);
21
24
}
22
-
getRandomInRange(4, 10)
25
+
getRandomInRange(4, 10);
23
26
// => 5.72
24
27
```
28
+
25
29
## Generating random integers
30
+
26
31
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
28
34
29
35
The `Math.random()` function should NOT be used for security and cryptographic applications!!
30
36
31
37
Instead, you can use the Web Crypto API, which provides various cryptographic functions.
"blurb": "Learn about randomness and the Math.random() function while helping Mary generate stardates and starship registry numbers for her Star Trek roleplay."
0 commit comments