Binge On Code

Jun 8, 2023

329 readers

How to create a javascript array of random numbers

It is very easy to create a javascript array of random numbers. We will use a few methods for this and a few lines of code to have this going!

At times you may find yourself needing to create a javascript array of random numbers. Well, it is not as difficult as it actually sounds. We will use completely built-in methods in javascript for this.

In order to create a javascript array of random numbers, we will take two steps. The first step will be to create the actual array and the other step will be to populate the array with the random numbers.

So, in our example, we will use a hypothetical array at the teller machine, which deals with random ticket numbers so that these numbers are then dealt to the user.

Such a javascript array of random numbers will only need to have positive integer numbers between a given max and a given min value.

1. Defining the javascript array.

The first thing we will need is the actual array that we need. This array will have a maximum number of ticket numbers, which is the same as the maximum ticket number possible. So, to do that, we will have it like so:

let maxTicketNumber = 1000;
let minTicketNumber = 0;
let ticketNumbers = Array(maxTicketNumber).fill(0);

The above code will give us a random ticketNumbers array, which will just be a shell, with as many empty slots as there can be tickets. Then we will use Array.fill() to populate our javascript array of random numbers with zeros initially.

2. Generating the random numbers.

Now, time for the fun part, using the Math module to create random numbers and populate out ticketNumbers javascript array of random numbers.

Since we already have the ticketNumbers array, which has empty slots, we will simply transform it into an array which actually has random slots instead. For that, we will use the Array.map() method.

Within our .map method, we will wrap the code which will transform our ticketNumbers into array of random numbers. Well, it is so simple, just like this:

let maxTicketNumber = 1000;
let minTicketNumber = 0;
let ticketNumbers = Array(maxTicketNumber).fill(0);

ticketNumbers.map(_ => {
  // our random generation code will go here
});

So, to example what array.map() does. It takes an input and transforms it into another things and returns that thing, so that the array we assign to the map result, should have the new things in it. That is just to summarize, which is out of the scope for this guide.

3. Adding javascript array of random numbers code.

So, we will now update the last code snippet and have it give us random numbers. This is where we get to use Math.random() and Math.floor() methods.

If you are not familiar with the above methods, we have a tutorial for this linked at the bottom of the page. However, our updated code should look like this:

let maxTicketNumber = 1000;
let minTicketNumber = 0;
let ticketNumbers = Array(maxTicketNumber).fill(0);

ticketNumbers = ticketNumbers.map(_ => {
  const seed = [Math.random() * minTicketNumber, Math.random() * maxTicketNumber];
  const randomValue = seed[Math.floor(Math.random() * seed.length)];
  return Math.round(randomValue)
});

console.log(ticketNumbers);

So, let’s understand what changed.

This line:

const seed = [Math.random() * minTicketNumber, Math.random() * maxTicketNumber];

Will give us an array of two numbers, a number dealt using the minTicketNumber and another dealt using maxTicketNumber.

Then we need a random value from the dealt values and that is what this line will do:

const randomValue = seed[Math.floor(Math.random() * seed.length)];

What this code does is that it picks a value from the seed, and it does so in a random manner, hence the reason why we have Math.floor(Math.random() * seed.length) as the array index.

This will give us a whole array index to use for the lookup. You may be tempted to use Math.ceil() but this can be problematic, since it rounds upwards and may give an index which is not there in out javascript array of random numbers.

Finally, we return the result from our .map() method:

return Math.round(randomValue);

To test this, go ahead and try logging the output:

console.log(ticketNumbers);

This will now give you a javascript array of random numbers.

Conclusion.

Well, as you can see, it was not that difficult to have a javascript array with random numbers.

Now that you can create array of random numbers in javascript, you have also learnt important methods from the Math module, as well as other array inbuilt methods.

Well, check out the previous article which may explain some of the concepts in details.

Happy coding!

Related Articles

Code Splitting And Lazy Loading

Code Splitting and lazy loading are basically augmentations of one another. Lazy loading builds on top of the power of code splitting.

Jun 8, 2023

Views 498

Destructure JavaScript Object With Defaults

You can really simplify your access to your JavaScript objects by using this simple method to destructure JavaScript object with defaults.

Jun 20, 2023

Views 27

Get Index Of Item In JavaScript For Of Loop

In a few lines of code, you will see how easy it is to get an item’s index in JavaScript For Of Loop. Let’s do this!

Jun 16, 2023

Views 34

Angular JavaScript React CSS Django Python React Native Next JS State Management Django Rest Framework Unity