rand | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

rand

How is possible to improve function function getRandomArbitary(min, max){ return Math.random() * (max - min) + min; } if i want to return 'max' variable as possible?

16th May 2018, 8:46 PM
Grigoriy
Grigoriy - avatar
5 Respuestas
+ 9
just add + 1 to (max - min) so it becomes (max - min + 1) and wrap it all with Math.floor() function randVal(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }
16th May 2018, 9:02 PM
Burey
Burey - avatar
+ 1
i came to this solution (with user input min and max and rounded number (you can remove those)) var max = prompt("max"); var min =prompt("min"); var x = Math.floor(Math.random(min)*max); document.write(x);
16th May 2018, 8:57 PM
Roel
Roel - avatar
0
sorry for my misunderstanding, but did you ment you want a random number between min and max?
16th May 2018, 8:53 PM
Roel
Roel - avatar
0
yes
16th May 2018, 8:54 PM
Grigoriy
Grigoriy - avatar
0
hues
22nd Aug 2018, 12:43 PM
Kordero Rey
Kordero Rey - avatar