Random Integers

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Random Integers

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Problem

/**
* @returns a random int between
* @param min inclusive
* @param max inclusive
*/
export function randomInt(min: number, max: number) {

}

Solution

/**
* @returns a random int between
* @param min inclusive
* @param max inclusive
*/
export function randomInt(min: number, max: number) {
return min + Math.floor(Math.random() * (max - min + 1));
}

Transcript

00:00 As we start to look at more

00:01 and more challenging programming interview questions,

00:03 we will end up needing more

00:04 and more utility functions to do little tasks for us.

00:07 One such task is the ability

00:09 to create random intes within a given range,

00:12 and that is a utility function

00:13 that we will create in this lesson.

00:15 And this is something that I've used even outside

00:17 of the context of a programming interview.

00:19 So let's go JavaScript run times provide a native function

00:24 for you to get a random number starting at zero,

00:27 but less than one.

00:29 This function is called math at random.

00:31 And here's an example of first

00:32 logging out five of these values.

00:34 So you get the general idea.

00:35 Now, this is a floating point number starting at zero,

00:38 but ending right before one.

00:40 And the objective is that you can multiply it

00:42 with the number of buckets that you want.

00:44 For example, if you were to multiply it with two,

00:47 you would essentially get two buckets.

00:49 One, starting at zero, but ending right before one

00:52 and a second bucket starting at one

00:54 and ending right before two.

00:56 Now with this example in mind, if our objective is

00:59 to get the integer zero

01:00 or one randomly, we can do that quite easily

01:03 with this current value by simply using math or flow.

01:06 And this will click the bucket zero to right

01:08 before one to zero and click the bucket one to right

01:11 before two, to the value one.

01:14 And when we run this, this is exactly what we see.

01:17 Now we're off to a good start.

01:18 If you wanted more intes beyond the values zero

01:21 and one, all that we really need to do is

01:23 to multiply with the bigger integer.

01:25 For example, if you want the values zero to 99, we have

01:29 to multiply with 100.

01:31 And of course, if you run this, we are going

01:33 to see the values that we expect.

01:35 Now continuing on a journey.

01:37 The next thing that we should probably solve is

01:40 that if we want the value 100 as well, then all

01:44 that we really need is one more bucket.

01:46 So we could just do a hundred plus one,

01:48 and now we should get the values in the range zero

01:51 to 100, both inclusive.

01:54 Now the next thing that we would want

01:56 to do in a generalized pattern would be

01:58 to shift the starting value.

02:00 Right now we are starting at the number zero,

02:02 but if you want to start at a different number, for example,

02:05 say the number 10, we can do that quite easily by adding 10

02:08 to the base value

02:09 and then removing 10 from the overall number

02:12 of buckets that we will generate.

02:14 And that is it. Now we have a generalized pattern

02:17 of creating an integer randomly starting at a particular

02:20 value inclusive and ending at a particular value inclusive.

02:24 But of course, writing this again

02:26 and again would not be something that would be practical.

02:28 So let's create a utility function that does the same thing

02:31 and takes a number of arguments.

02:33 We have this function random int that takes a minimum number

02:36 and a maximum number,

02:38 and then does exactly what we demonstrated previously,

02:40 but this time by using the past in arguments.

02:43 And we can have a quick look that it is working as expected

02:46 by logging out a hundred values in the range,

02:49 one to five inclusive.

02:51 And when we run this, you can see the numbers

02:53 1, 2, 3, 4, 5, all on screen.

02:56 Now I know that there is a lot to take in, so let's

02:58 Do a quick recap.

03:00 We take math or random

03:01 and scale it into a number of buckets,

03:03 which we've determined to be max minus min plus one.

03:07 We floated down to the integer with math or flow,

03:10 and finally scale it from the bottom by adding min.

03:14 Now, one more quick tip that I have

03:16 for you from the programming interview perspective is

03:18 that even though you could write this randomization in line,

03:21 it's a good idea to do it as a utility function, as in case

03:24 of any issues, you can just debug this

03:26 and additionally, it gives you something to talk about.