how to make a random number generator? (JS) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

how to make a random number generator? (JS)

Please help me. as simple as possible (JS)

20th Jul 2019, 9:50 AM
Мичес
Мичес - avatar
5 Answers
+ 4
This is the function for you: Math.random. It generates a random bumber between 0 (inclusive) and 1 (exclusive). You can get a random number between two numbers with this "formula": Math.random() * (max - min) + min
20th Jul 2019, 10:00 AM
Airree
Airree - avatar
+ 2
You can use that formula but sometimes you will get double result. You need to use Math.ceil().Math.floor() to change them into int
20th Jul 2019, 10:07 AM
Dwi Novianto Nugroho
Dwi Novianto Nugroho - avatar
+ 2
in cs...  Random num = new Random();             int x = num.Next(0, 10);             it will generate  a number between 0 and 9, but not 10.
20th Jul 2019, 10:12 AM
laura
+ 1
Thank youu
20th Jul 2019, 10:02 AM
Мичес
Мичес - avatar
+ 1
In java also Random r = new Random(); int rand = r.nextInt(10)
20th Jul 2019, 10:29 AM
Dwi Novianto Nugroho
Dwi Novianto Nugroho - avatar