How to Generate Random Numbers in JavaScript - Math.random

preview_player
Показать описание


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
Рекомендации по теме
Комментарии
Автор

use document.write(random); to actually display the number on the screen.

quickeasycoding
Автор

//The maximum is inclusive and the minimum is inclusive
function getRandomIntInclusive() {
let min = 0;
let max = 10;
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}

ChrisTian-oxnr
Автор

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;

ogaburan
Автор

THANK YOU VERY MUCH IF IT WORKED FOR ME thank you very much.

Carlitos_
Автор

thanks for this tutorial, easy to understand. Good work!!

lucalion
Автор

Why have you got some dislikes ! Everything is clear ! so good job :)

anaisrevellat
Автор

I would have liked a better description on why you add the 1 at the end. I liked that you removed some pieces of code to show what happens when you remove the Math.floor and the +1, but you don't explain why you add 1. Also why did you multiply by 10? For being high on YouTube's result list I'm left with questions still.

josephcastrigno
Автор

Wouldn't this be easier understood for beginners with Math.ceil instead of floor? This way you do not have to add one.

matthewhayes
Автор

is their away to make multiple generators without them interfering each other

GingerMinaster
Автор

What I want to know is how to log that. I want to create an RNG that I can log for my group.

FlockofSmeagles
Автор

But what if you want to print numbers between 71 and 89?

ankurparchani
Автор

Instructions unclear made all my friends broke from this lol

masterdyl
Автор

Sir i have one doubt how to display graph randomly ????

shwethadevadiga
Автор

well, i have a queston about it...

.
.
.
.

I just type

var random = Math.floor(Math.random() * 10) +1;


pengabdikedamaian
Автор

Hi its possible that i can get a random number via time base which is every 12am random number will be executed?

epi
Автор

Good explanation but what about if we want a number say between 20 and 30? How do you start it not at 1?

bluffer
Автор

How can I include a Number in the result?

jacklujimmy
Автор

He adds 1 so the number can, t be zero if it, s zero there will be 1 added n it so 0+1 = 1 so it will never be zero and the number will be between 1 and 10 not 0 and 10

tauheedhussain
Автор

1 isn't included just >=0 && <1

riomilano
Автор

hello, actually i have generated the random numbers but i have to put the numbers in a table using javascript and after creating the table how am i suppose to insert it .? can u plz help .??

darshanabhuyan