1
00:00:02,430 --> 00:00:09,000
Before we move on to strings, here's a little demo with the math object and a little convenience function

2
00:00:09,000 --> 00:00:13,070
which you might actually need in many of your programs.

3
00:00:13,140 --> 00:00:16,060
We have math random to give us a random number,

4
00:00:16,080 --> 00:00:22,200
now what does math random produce though? It produces a random number between 0 and 1.

5
00:00:22,210 --> 00:00:26,950
Now that's nice but sometimes you need a random number between 10 and 20,

6
00:00:27,000 --> 00:00:29,470
what can you do in such a scenario?

7
00:00:29,490 --> 00:00:35,970
Well we can write our own function that builds up on math random and allows us to provide an upper and

8
00:00:35,970 --> 00:00:37,680
lower boundary.

9
00:00:37,680 --> 00:00:42,870
Now for that, I'll create a new function and of course you can create it in any way you want, with a function

10
00:00:42,870 --> 00:00:49,830
declaration, function expression, arrow function, whatever you need and I'll name it randomIntBetween,

11
00:00:49,890 --> 00:00:52,260
now of course the name is totally up to you.

12
00:00:52,320 --> 00:00:55,900
Now here I expect two values, a minimum and a maximum value,

13
00:00:55,920 --> 00:01:00,540
so my lower and my upper boundary and we want to return a number here.

14
00:01:00,540 --> 00:01:06,900
Now it all starts with math random but of course, that is between 0 and 1 as we learned and that's not

15
00:01:06,900 --> 00:01:08,340
what we want here.

16
00:01:08,430 --> 00:01:12,610
Now to respect our minimum boundary, we can add the minimum,

17
00:01:12,720 --> 00:01:20,010
if we call this function for example with a value of 5 for min, we now get a random number plus our minimum,

18
00:01:20,010 --> 00:01:23,950
so we can't get a number lower than min because we add min,

19
00:01:24,030 --> 00:01:29,000
so we're already respecting our minimum which sounds quite good.

20
00:01:29,040 --> 00:01:31,410
Now we also of course want to respect the max

21
00:01:31,410 --> 00:01:39,370
however. Now to respect the max here, we need to multiply our random number here.

22
00:01:39,500 --> 00:01:45,850
What happens if we multiply it with our maximum? Let's say our minimum is 5, our maximum is 10,

23
00:01:45,860 --> 00:01:49,030
this gives us a random number between 0 and 1,

24
00:01:49,130 --> 00:01:51,310
we multiply this with 10,

25
00:01:51,470 --> 00:01:58,540
So if we have 0 which is of course very unlikely, then we would stick to 0, we add min,

26
00:01:58,550 --> 00:02:01,130
we have our minimum number, sounds about right.

27
00:02:01,130 --> 00:02:05,080
If we have one, then we multiply this by max,

28
00:02:05,090 --> 00:02:06,790
so if our max value was 10,

29
00:02:06,860 --> 00:02:10,390
now we have 10 and we add 5 to it,

30
00:02:10,400 --> 00:02:14,240
so now we would have a value of 15 which is above our maximum.

31
00:02:14,240 --> 00:02:18,470
So we actually need to multiply it with max minus min,

32
00:02:18,470 --> 00:02:22,210
so only with the range between max and min.

33
00:02:22,220 --> 00:02:27,960
Now if max is 10 and I can put this here to make it easier to keep in mind,

34
00:02:27,980 --> 00:02:32,300
so these are the values we're working with in our fictional example to verify the function.

35
00:02:32,420 --> 00:02:36,270
If min is 5 and max is 10, then this yields 5.

36
00:02:36,380 --> 00:02:43,010
If random, math random gives us 1, then this overall term produces 1 times 5 equals 5,

37
00:02:43,100 --> 00:02:47,270
we add 5 to that, we have 10 which is of course our maximum

38
00:02:47,270 --> 00:02:50,710
so that is all right. Now theoretically, that would be great

39
00:02:50,960 --> 00:02:57,260
but even though it's not saying it here at the tooltip but you could find it on MDN for example, it's

40
00:02:57,260 --> 00:03:05,090
important to understand that for the random method here on the math object, 1 is actually not included,

41
00:03:05,090 --> 00:03:11,590
so the random number we'll get might be 0.99999 but we'll never get a 1.

42
00:03:11,690 --> 00:03:17,750
So as a result, the biggest number we can get here is max minus min,

43
00:03:17,780 --> 00:03:22,120
so that's 5, 5 times 0.99999,

44
00:03:22,190 --> 00:03:28,000
so that's almost 5, plus 5 is almost 10.

45
00:03:28,010 --> 00:03:30,770
Now if we want to include both the minimum and the maximum here,

46
00:03:30,770 --> 00:03:33,500
so we want to make sure that both numbers can occur,

47
00:03:33,500 --> 00:03:39,680
then we're not finished yet because right now this can never yield the maximum because random only yields

48
00:03:39,860 --> 00:03:44,430
0.99999 and therefore we can only get close to the maximum.

49
00:03:44,720 --> 00:03:52,910
Now to get the maximum, we can add one here to our multiplication term so that this actually is 6 for these

50
00:03:52,910 --> 00:03:58,810
values we're providing here and therefore the biggest number we can reach right now is 

51
00:03:58,810 --> 00:03:59,910
10.99999,

52
00:03:59,990 --> 00:04:00,310
right,

53
00:04:00,320 --> 00:04:04,240
because we have six times almost 1,

54
00:04:04,340 --> 00:04:09,470
so we have almost 6 as a maximum result here, plus min which is 5,

55
00:04:09,470 --> 00:04:15,740
we have almost 11. Now of course we want to stop at max however which is 10 and not at almost 11

56
00:04:16,190 --> 00:04:21,770
and now we can leverage another function on the math object and that's the floor function. Floor

57
00:04:21,890 --> 00:04:28,280
rounds this number down to the nearest integer, so we can wrap this entire term with math floor and it

58
00:04:28,280 --> 00:04:34,130
will basically just cut the decimal places you could say. So if we had this as a result,

59
00:04:37,750 --> 00:04:40,630
then floor will turn this into 10

60
00:04:40,630 --> 00:04:47,470
and now we have a function which yields a number between min and max where both values are included.

61
00:04:47,530 --> 00:04:52,480
Now of course you can deviate that and you can play around with that and if you google for Javascript,

62
00:04:52,510 --> 00:04:59,140
random between and so on, you'll find many examples where min is not included, where max is not included

63
00:04:59,200 --> 00:05:00,230
and so on

64
00:05:00,280 --> 00:05:04,220
but this here is a pretty common scenario which is why I wanted to show it to you.

65
00:05:04,390 --> 00:05:11,500
Well and with that if we give it a try and we console log random in between let's say 1 and 10, let's

66
00:05:11,500 --> 00:05:12,850
see what that gives us.

67
00:05:12,850 --> 00:05:19,600
If we save that and we go back to our page and reload, now we should get a couple of integers randomly

68
00:05:20,050 --> 00:05:24,320
but never above 10 or below 1.

69
00:05:24,340 --> 00:05:26,790
So now we can run this as often as we want,

70
00:05:26,830 --> 00:05:30,880
we'll never get a number bigger than 10 or smaller than 1.
