JavaScript makes it really easy to generate and use pseudo-random numbers. By using the built-in Math.random() function, you are able to achieve just that.

Math.random() will give you a random floating point number from 0 to 1 (non-inclusive of 1) and so to convert that result into a whole number, you’ll need to implement some basic math.

In this video I show you a simple example of how to generate a random number and also how to generate a random whole number (or integer) between a range of 1 and X.

For your reference, check this out:

Support me on Patreon:

Follow me on Twitter @dcode!

If this video helped you out and you’d like to see more, make sure to leave a like and subscribe to dcode!

#dcode #javascript #webdev

Nguồn: https://mensagensdamanha.com/

Xem thêm bài viết khác: https://mensagensdamanha.com/cong-nghe/

Perfect – thanks!

Good tutorial, thanks

Thanks man, you saved my life with this! I want to create a game where you have 50% chance of death, and it really helped. thanks man!!!!!!

Why the * 1 at the end ?

whats the use of +1 ????

Well explained. Thank you.

👍👍

Hi Dom, I have a question on this while I was looking through it. I don't know why it's more common to see Math.floor() more than Math.round(). Why is that? Does anyone know?

For people who want to do random numbers between two numbers that are not 1 to whatever, here is an example:

var minAge = 14 ;

var maxAge = 36-minAge ;

var Age = Math.floor(Math.random() * maxAge ) + minAge +1;

seems like this could be useful for creating a hash function, is that a thing?

How can I do a random of one list of numbers? For example 3 3 3 5 6 7 0 0 0 0 and no repeat please

Like your intro..wow