Generate random numbers in JavaScript
In this blog, you will learn how to generate random numbers in javascript.
There is no straightforward way to generate a random number. You have to use a little trick.
Math.random
function gives us a random number between 0 and 1.
const getRandomNum = () => {reutrn Math.random()}console.log(getRandomNum())console.log(getRandomNum())console.log(getRandomNum())console.log(getRandomNum())// output// 0.4656524910632813// 0.36946529666025185// 0.93309610877532// 0.6869999317373736
That's not what you want. You need an integer. To get an integer you need to multiply the random number with another number. And then you have to floor the value.
Get a random integer
const random = Math.randomconst floor = Math.floorconst getRandomNum = () => {return floor(random() * 10)}setInterval(() => {const num = getRandomNum()console.log(num)}, 500)// Output:// 2// 5// 1// 9// 1// 7// 6// 0// 7// 3// 4// 8// 2// 2// 4// 5// 5// 0
By multiplying a number you have set an upper limit for the random number. We have floored the value because we don't want a floating-point number. Math.floor function gives us this
8.343487294263 --> 83.37294263 --> 3
Notice that we never get 10 as our random number. Because that is our limit. To include 10, just add 1
to the number.
Get a random integer in a range
Let's see how we can get a random number between 10 and 20
const random = Math.randomconst floor = Math.floorconst getRandomNumInRange = (min, max) => {return floor(random() * (max - min + 1)) + min}setInterval(() => {const num = getRandomNumInRange(10, 20)console.log(num)}, 500)// output:// 11// 13// 14// 20// 13// 13// 10// 16// 17// 10// 16
Explanation: Now we need something between two numbers. So, we generate random numbers up to the difference of our range. In our case, it will be
difference = 10random numbers = [2,3,4,6,10 ...]
Then we add our minimum number with a random number. And that number definitely will be in the range.
You can write the code in one line.
const getRandomInt = (min, max) =>Math.floor(Math.random() * (max - min + 1)) + min
That's how we get a random number in a range.
Shameless Plug
Want to create your own blog? Well, I am creating a video series where you will learn about how to create a JAMstack blog with Nextjs and Chakra-UI.
Lessons
- Intro & Setup
- Build Homepage UI
- How our app will work
- MDX, MongoDB, Static Homepage
- Generate Static Blog Page
- Style Blog page with Chakra-UI and MDX-embed
- Build a real-time view counter
- Autocomplete search form with MongoDB Atlas Search Index
- Deploy application to Vercel
Demo
You can demo the website from here
Features
- Static Blog pages will make the website load faster.
- Blogs will have code blocks with syntax highlighting and many embed components like youtube videos, GitHub gist, Tweets, and so many other things.
- Autocomplete search feature for the blog posts.
- Real-time view counter and so on.
Please like and subscribe to Cules Coding. It motivates me to create more content like this.
That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.
By the way, I am looking for a new opportunity in a company where I can provide great value with my skills. If you are a recruiter, looking for someone skilled in full-stack web development and passionate about revolutionizing the world, feel free to contact me. Also, I am open to talking about any freelance project. I am available on Upwork
Contacts
- Email: thatanjan@gmail.com
- LinkedIn: @thatanjan
- Portfolio: anjan
- Github: @thatanjan
- Instagram (personal): @thatanjan
- Twitter: @thatanjan
- Upwork: @thatanjan
Blogs you might want to read:
- Eslint, prettier setup with TypeScript and react
- What is Client-Side Rendering?
- What is Server Side Rendering?
- Everything you need to know about tree data structure
- 13 reasons why you should use Nextjs
- Beginners guide to quantum computers
Videos might you might want to watch: