I want to create a random number generator using JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I want to create a random number generator using JavaScript?

The array must contains 23 x '1', 4 X number '2', 7 x' 5' 4 x '10', two strings 'chance' three strings 'tworolls' and one string called '4 rolls'

6th May 2020, 6:51 AM
Tharun
Tharun - avatar
5 Answers
+ 3
<!DOCTYPE html> <html> <head> <title>random number in javascript</title> </head> <body> <script> //get random number var rndnmb=Math.floor(Math.random()*100); document.write("<b>the random number is </b>"+rndnmb); </script> </body> </html> Try it
7th May 2020, 5:38 PM
Humoyun
Humoyun - avatar
+ 2
Tharun Here is the solution : Code : const array = [ /*your array */ ]; const randomIndex = Math.floor(Math.random() * array.length); const randomElem = array[randomIndex]; console.log(randomElem); Here is a demo : https://code.sololearn.com/W54yd8ILd5AT/?ref=app
6th May 2020, 8:17 AM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 1
Hey Tharun, I didn’t understand your description. Please explain a bit more :))
6th May 2020, 7:34 AM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 1
Array =[ 1, 1, 1,......(23 times) 2, 2,.....(14 times), 5, 5,...(7 times), 10, 10, 10, 10, '2rolls', '2rolls', '2rolls', 'chance', 'chance', '4roll', ] I want to generate random elements from this array using JavaScript
6th May 2020, 8:15 AM
Tharun
Tharun - avatar
0
May I know how this is not working. I want random element to go on a loop continuously let Array =[ "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "5", "5", "5", "5", "5", "5", "5", "10", "10", "10", "10", "2roll", "2roll", "2roll", "chance", "chance", "4roll", ] var t = 0; var i = 10; const randomIndex = Math.floor(Math.random() * Array.length); const randomElem = Array[randomIndex]; console.log(randomElem); for (var element of randomElem ) { if(element ===1) { t = t + i; console.log(element ) console.log(t) console.log(i) } else { t = t - i; i = i * 2; console.log(element ) console.log(t) console.log(i) } }
8th May 2020, 7:52 AM
Tharun
Tharun - avatar