Skip to content

Commit 1d91100

Browse files
jwunderlriknoll
andauthored
update random range to randint (#2993)
* update random range * fix typo I noticed while sanity checking Co-authored-by: Richard Knoll <[email protected]>
1 parent a090b41 commit 1d91100

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+368
-367
lines changed

docs/blocks/logic/boolean.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ When you compare two Numbers, you get a Boolean value, such as the comparison `x
1818

1919
```blocks
2020
input.onButtonPressed(Button.A, () => {
21-
let x = Math.randomRange(0, 5)
21+
let x = randint(0, 5)
2222
if(x < 5) {
2323
basic.showString("low");
24-
} else {
24+
} else {
2525
basic.showString("high");
2626
}
2727
})

docs/courses/blocks-to-javascript/complex-conditionals.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Sometimes there is code you want to run if two or more conditions are true. The ``||logic:if then||`` block lets you combine conditions with the ``||logic:and||`` and the ``||logic:or||`` blocks. For example, if we want to see if a random number is between `0` and `9`, we can put two conditions in an ``||logic:if then||`` and connect them with an ``||logic:and||``.
66

77
```blocks
8-
let rando = Math.randomRange(-20, 20)
8+
let rando = randint(-20, 20)
99
if (rando >= 0 && rando < 10) {
1010
basic.showNumber(rando)
1111
}
@@ -14,7 +14,7 @@ if (rando >= 0 && rando < 10) {
1414
This is how to check if a result is within a range of values, in this case it's from `0` to `9`. What if we want to include the values between `15` and `18` too? We just add another range check and connect them with an ``||logic:or||``.
1515

1616
```blocks
17-
let rando = Math.randomRange(-20, 20)
17+
let rando = randint(-20, 20)
1818
if ((rando >= 0 && rando < 10) || (rando >= 15 && rando < 18)) {
1919
basic.showNumber(rando)
2020
}
@@ -23,7 +23,7 @@ if ((rando >= 0 && rando < 10) || (rando >= 15 && rando < 18)) {
2323
Having all of these conditions in one ``||logic:if then||`` makes a large and complicated block. It can be hard to see how the complete conditional does both of the range checks. At times, it's easier to read the code in the JavaScript editor to better see the order and connections of the conditions.
2424

2525
```typescript
26-
let rando = Math.randomRange(-20, 20)
26+
let rando = randint(-20, 20)
2727
if ((rando >= 0 && rando < 10) || (rando >= 15 && rando < 18)) {
2828
basic.showNumber(rando)
2929
}
@@ -36,7 +36,7 @@ As you can see, reading the code for this conditional is might seem easier in Ja
3636
If one ``||logic:if then||`` block is placed inside another, then each condition for checking the random number is placed in its own ``||logic:if then||``. This give the same result as before with the two conditions in the ``||logic:if then||``.
3737

3838
```block
39-
let rando = Math.randomRange(-20, 20)
39+
let rando = randint(-20, 20)
4040
if (rando >= 0) {
4141
if (rando < 10) {
4242
basic.showNumber(rando)
@@ -47,7 +47,7 @@ if (rando >= 0) {
4747
When we switch the editor to JavaScript view, we see that the second ``if ()`` statement is indented and contained inside the `{ }` of the first. This matches the second ``||logic:if then||`` block that fits inside the first one. These are called _nested_ if statements.
4848

4949
```typescript
50-
let rando = Math.randomRange(-20, 20)
50+
let rando = randint(-20, 20)
5151
if (rando >= 0) {
5252
if (rando < 10) {
5353
basic.showNumber(rando)
@@ -138,4 +138,4 @@ To set the variable based on the result of the expression, the value options are
138138
let heatMessage = input.temperature() < 10 ? "COLD" : "WARM"
139139
```
140140

141-
The ``heatMeassage`` variable is set to the string saying ``"COLD"`` if the temperature is less than `10` degrees celsius or to ``"WARM"`` when it's `10` degrees or warmer.
141+
The ``heatMeassage`` variable is set to the string saying ``"COLD"`` if the temperature is less than `10` degrees celsius or to ``"WARM"`` when it's `10` degrees or warmer.

docs/courses/csintro/arrays/activity.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ Create an array of words that can be used as part of a charades-type game.
66
This activity is based on a very popular phone app invented by Ellen DeGeneres (https://bits.blogs.nytimes.com/2013/05/03/ellen-degeneres-iphone-game/).
77

88
![Heads up game](/static/courses/csintro/arrays/headband-charades.png)
9-
9+
1010
* Create a new variable and give it a name like arrayWords.
11-
* Insert a 'set' variable block into the 'on start' block.
11+
* Insert a 'set' variable block into the 'on start' block.
1212
* Change the default variable name to this new variable name.
1313
* From the Array Toolbox drawer, drag an 'array of' block to the coding workspace.
14-
* Attach this array block to the end of the 'set' variable block.
14+
* Attach this array block to the end of the 'set' variable block.
1515

1616
```blocks
1717
let arrayWords = ["", ""]
@@ -28,12 +28,12 @@ let arrayWords = ["", "", "", "", "", "", ""]
2828
```
2929
* Fill each string with one word. Choose words that will be fun for a game of charades. Example:
3030

31-
```blocks
31+
```blocks
3232
let arrayWords = ["cat", "guitar", "flashlight", "cupcake", "tree", "frisbee"]
3333
```
3434

3535
Now, we need a way to access one word at a time from this array of words.
36-
* We can use the 'show string' block from the Basic Toolbox drawer, and the 'on screen up' event handler from the Input Toolbox drawer (this is a drop-down menu choice of the 'on shake' block) to tell the micro:bit to display a word when we tilt the micro:bit up.
36+
* We can use the 'show string' block from the Basic Toolbox drawer, and the 'on screen up' event handler from the Input Toolbox drawer (this is a drop-down menu choice of the 'on shake' block) to tell the micro:bit to display a word when we tilt the micro:bit up.
3737
* For this version, we’ll display the words one at a time in the order they were first placed into the array.
3838
* We’ll use the index of the array to keep track of what word to display at any given time, so you'll need to create an 'index' variable.
3939

@@ -89,7 +89,7 @@ input.onGesture(Gesture.ScreenDown, () => {
8989
We have a limited number of elements in our array, so to avoid an error, we need to check and make sure we are not already at the end of the array before we change the index.
9090

9191
* Under the Arrays Toolbox drawer, drag out a 'length of' block. The 'length of' block returns the number of items (elements) in an array. For our array, the length of block will return the value 6.
92-
* But because computer programmers start counting at zero, the index of the final (6th) element is 5.
92+
* But because computer programmers start counting at zero, the index of the final (6th) element is 5.
9393

9494
Some pseudocode for our algorithm logic:
9595
* When the player places the micro:bit screen down:
@@ -141,9 +141,9 @@ input.onGesture(Gesture.ScreenDown, () => {
141141
## Game Play
142142
There are different ways you can play charades with our program. Here is one way you can play with a group of friends.
143143

144-
* With the micro:bit on and held so Player A cannot see the screen, another player starts the program to see the first word.
144+
* With the micro:bit on and held so Player A cannot see the screen, another player starts the program to see the first word.
145145
* The other players act out this word charades-style for Player A to guess.
146-
* When Player A guesses correctly or decides to pass on this word, a player places the micro:bit screen down.
146+
* When Player A guesses correctly or decides to pass on this word, a player places the micro:bit screen down.
147147
* When ready for the next word, a player turns the micro:bit screen up. Play continues until all the words in the array have been used.
148148

149149
## Mod this!
@@ -197,15 +197,15 @@ Review the use of the random block in the Math category.
197197
* Create a block that will plot a single dot at a random location on the screen by choosing a random number from 0 to 4 for the x axis and a random number from 0 to 4 for the y axis.
198198

199199
```blocks
200-
led.plot(Math.randomRange(0, 5), Math.randomRange(0, 5))
200+
led.plot(randint(0, 4), randint(0, 4))
201201
```
202202

203203
Next, let’s create a loop that will repeat the above code five times, for a constellation with five stars.
204204

205205
```blocks
206206
for (let index = 0; index <= 4; index++) {
207-
led.plot(Math.randomRange(0, 5), Math.randomRange(0, 5))
208-
}
207+
led.plot(randint(0, 4), randint(0, 4))
208+
}
209209
```
210210

211211
Note that to keep things simple we don’t check for duplicates, so it’s possible you may end up with fewer than five visible stars. That’s okay.
@@ -233,13 +233,13 @@ To fix this, we need to do a little math by subtracting 1 from whatever the valu
233233
let list = [5, 2, 1, 3, 4]
234234
235235
for (let index = 0; index < list[0] - 1; index++) {
236-
led.plot(Math.randomRange(0, 5), Math.randomRange(0, 5))
236+
led.plot(randint(0, 4), randint(0, 4))
237237
}
238238
```
239239

240240
The above code takes the first value in the array and creates that many stars at random locations. Now, all we need to do is iterate through the entire array and do the same thing for each of the values. We will put the above code inside another loop that will run the same number of times as the length of the array (in this case, 5). You should also use a 'pause' block and a 'clear screen' block in between each constellation.
241241

242-
Finally, you might attach the code that shows the constellations to an 'on button A pressed' event handler.
242+
Finally, you might attach the code that shows the constellations to an 'on button A pressed' event handler.
243243

244244
Here is the complete program.
245245

@@ -248,7 +248,7 @@ let list: number[] = []
248248
input.onButtonPressed(Button.A, () => {
249249
for (let i = 0; i <= list.length - 1; i++) {
250250
for (let j = 0; j <= list[i] - 1; j++) {
251-
led.plot(Math.randomRange(0, 5), Math.randomRange(0, 5))
251+
led.plot(randint(0, 4), randint(0, 4))
252252
}
253253
basic.pause(1000)
254254
basic.clearScreen()

docs/courses/csintro/arrays/unplugged.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ In this activity, you will demonstrate different kinds of sorting methods on you
1818
* Ask the Sorter to place the students in order by directing them to move, one at a time, to the proper place.
1919
* Once the students are sorted, ask students the following:
2020

21-
>*  How did she sort you into the right order?
22-
* Did you see a pattern?
21+
>*  How did she sort you into the right order?
22+
* Did you see a pattern?
2323
* What did she do?
2424

2525
Try to get students to be as precise as possible in explaining their thinking. Sometimes it helps to put the steps on the board, in an algorithm:
@@ -62,7 +62,7 @@ let column = 0
6262
let index = 0
6363
input.onButtonPressed(Button.AB, () => {
6464
for (let index = 0; index <= 4; index++) {
65-
list[index] = Math.randomRange(0, 5) + 1
65+
list[index] = randint(0, 5) + 1
6666
}
6767
})
6868
input.onButtonPressed(Button.B, () => {
@@ -142,7 +142,7 @@ input.onButtonPressed(Button.B, () => {
142142
})
143143
input.onButtonPressed(Button.AB, () => {
144144
for (let index = 0; index <= 4; index++) {
145-
list[index] = Math.randomRange(0, 5) + 1
145+
list[index] = randint(0, 5) + 1
146146
}
147147
})
148148
input.onButtonPressed(Button.A, () => {
@@ -223,7 +223,7 @@ input.onButtonPressed(Button.A, () => {
223223
})
224224
input.onButtonPressed(Button.AB, () => {
225225
for (let index = 0; index <= 4; index++) {
226-
list[index] = Math.randomRange(0, 5) + 1
226+
list[index] = randint(0, 5) + 1
227227
}
228228
})
229229
input.onButtonPressed(Button.B, () => {

docs/courses/csintro/booleans/project.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![Two-Player Game Example Board](/static/courses/csintro/booleans/two-player.jpg)
44

5-
This is an assignment for students to come up with a micro:bit program that uses Boolean variables, Boolean operators, and possibly the random function.
5+
This is an assignment for students to come up with a micro:bit program that uses Boolean variables, Boolean operators, and possibly the random function.
66

77
## Input
88
Remind the students of all the different inputs available to them through the micro:bit.
@@ -205,7 +205,7 @@ let player1Turn = false
205205
let spin = 0
206206
let delay = 0
207207
input.onGesture(Gesture.Shake, () => {
208-
if (player1Turn == true && Math.randomRange(0, 4) < 3) {
208+
if (player1Turn == true && randint(0, 4) < 3) {
209209
basic.clearScreen()
210210
delay = 0
211211
while (delay < 500) {
@@ -250,7 +250,7 @@ input.onGesture(Gesture.Shake, () => {
250250
} else if (player1Turn) {
251251
basic.showString("Crash!")
252252
player1Turn = false
253-
} else if (Math.randomRange(0, 4) < 3) {
253+
} else if (randint(0, 4) < 3) {
254254
basic.clearScreen()
255255
delay = 0
256256
while (delay < 500) {
@@ -298,7 +298,7 @@ input.onGesture(Gesture.Shake, () => {
298298
}
299299
})
300300
basic.forever(() => {
301-
301+
302302
})
303303
delay = 0
304304
spin = 0
@@ -314,7 +314,7 @@ Here is a portion of the board game's code. A boolean variable is used to determ
314314
```blocks
315315
let player1Turn = false;
316316
input.onGesture(Gesture.Shake, () => {
317-
if (player1Turn == true && Math.randomRange(0, 4) < 3) {
317+
if (player1Turn == true && randint(0, 4) < 3) {
318318
319319
}
320320
})
@@ -353,4 +353,4 @@ Have students write a reflection of about 150–300 words, addressing the follow
353353
**4 =** Reflection piece addresses all prompts.<br/>
354354
**3 =** Reflection piece lacks 1 of the required elements.<br/>
355355
**2 =** Reflection piece lacks 2 of the required elements.<br/>
356-
**1 =** Reflection piece lacks 3 of the required elements.
356+
**1 =** Reflection piece lacks 3 of the required elements.

docs/courses/csintro/conditionals/activity.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Activity: Rock, paper, scissors
22

3-
For this activity, each student will need a micro:bit.
3+
For this activity, each student will need a micro:bit.
44
Everyone will create the same program, the classic rock paper scissor game.
55

66
![Rock, paper, scissors](/static/courses/csintro/conditionals/rock-paper-scissors-items.png)
@@ -14,7 +14,7 @@ Everyone will create the same program, the classic rock paper scissor game.
1414
>Example pseudocode:<br/>
1515
On button A press: choose random number from 0-2
1616
If random number = 0, then display rock icon,
17-
Else if random number = 1, then display paper icon,
17+
Else if random number = 1, then display paper icon,
1818
Else display scissors icon.
1919
* Point out that because there are only three possibilities, we don’t need to do a separate check to see if random number = 2. So we just use an else.
2020

@@ -34,7 +34,7 @@ Here's an example mod:
3434
```blocks
3535
let hand = 0
3636
input.onGesture(Gesture.Shake, () => {
37-
hand = Math.randomRange(0, 3)
37+
hand = randint(0, 3)
3838
if (hand == 0) {
3939
basic.showLeds(`
4040
# # # # #

0 commit comments

Comments
 (0)