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));
}
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

As we start to look at more and more challenging programming interview questions, we will end up needing more and more utility functions to do little tasks for us. One such task is the ability to create random intes within a given range, and that is a utility function that we will create in this lesson. And this is something that I've used even outside of the context of a programming interview. So let's go JavaScript run times provide a native function for you to get a random number starting at zero, but less than one. This function is called math at random. And here's an example of first

00:32

logging out five of these values. So you get the general idea. Now, this is a floating point number starting at zero, but ending right before one. And the objective is that you can multiply it with the number of buckets that you want. For example, if you were to multiply it with two, you would essentially get two buckets. One, starting at zero, but ending right before one and a second bucket starting at one and ending right before two. Now with this example in mind, if our objective is to get the integer zero or one randomly, we can do that quite easily with this current value by simply using math or flow.

01:06

And this will click the bucket zero to right before one to zero and click the bucket one to right before two, to the value one. And when we run this, this is exactly what we see. Now we're off to a good start. If you wanted more intes beyond the values zero and one, all that we really need to do is to multiply with the bigger integer. For example, if you want the values zero to 99, we have to multiply with 100. And of course, if you run this, we are going to see the values that we expect. Now continuing on a journey. The next thing that we should probably solve is

01:40

that if we want the value 100 as well, then all that we really need is one more bucket. So we could just do a hundred plus one, and now we should get the values in the range zero to 100, both inclusive. Now the next thing that we would want to do in a generalized pattern would be to shift the starting value. Right now we are starting at the number zero, but if you want to start at a different number, for example, say the number 10, we can do that quite easily by adding 10 to the base value and then removing 10 from the overall number of buckets that we will generate.

02:14

And that is it. Now we have a generalized pattern of creating an integer randomly starting at a particular value inclusive and ending at a particular value inclusive. But of course, writing this again and again would not be something that would be practical. So let's create a utility function that does the same thing and takes a number of arguments. We have this function random int that takes a minimum number and a maximum number, and then does exactly what we demonstrated previously, but this time by using the past in arguments. And we can have a quick look that it is working as expected by logging out a hundred values in the range,

02:49

one to five inclusive. And when we run this, you can see the numbers 1, 2, 3, 4, 5, all on screen. Now I know that there is a lot to take in, so let's Do a quick recap. We take math or random and scale it into a number of buckets, which we've determined to be max minus min plus one. We floated down to the integer with math or flow, and finally scale it from the bottom by adding min. Now, one more quick tip that I have for you from the programming interview perspective is that even though you could write this randomization in line, 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 and additionally, it gives you something to talk about.