Random Number Generation

Hi all. I just started here with the JS curriculum. When I did the first "game"ish exercise, WordBlanks, I thought it needed a little more oomph. So I added a Random Number Generator using Math.random. I threw in Math.round and a mulltiplier to give me random whole numbers between 0-9 for array picking.
With this version you get a new sentence etime you refresh the page. Check it out and let me know what you think! Positively, Diem

// MadBlanks-A random combination of MadLibs & WordBlanks
let adjectiveList=["adorable","blushing","clever","dangerous","beautiful","meaningless","gorgeous","sleeveless","obnoxious","colorful"];
let nounList=["dog","nova","cat","teleporter","bird","choo choo train","fish","hydraulic dam","microscope","atomic nucleus"];
let verbList=["exploded","collapsed","shrank","warped","dropped","flew","hovered","dug","minced","baked"];
let adverbList=["violently","exponentially","wondrously","unexpectedly","fantastically","surprisingly","astonishingly","admirably","slowly","steadfastly"];

var anAdjective = adjectiveList[Math.round(Math.random()*10)];
var aNoun = nounList[Math.round(Math.random()*10)];
var aVerb = verbList[Math.round(Math.random()*10)];
var anAdverb= adverbList[Math.round(Math.random()*10)];

madBlanks = "The "+anAdjective+" "+aNoun+" "+aVerb+" "+anAdverb + ".";


there is a commonly recognized method to receive a random number in a range. Its similar to your approach, but it uses Math.floor() instead, which makes for more just random numbers

Thank you for the comment. I’m here to learn, and anything that will make my code “tighter” and more efficient is a good thing!
The write-up at Math.floor() - JavaScript | MDN doesn’t say anything about randomness, and their JavaScript Demo: Math.floor() seems to generate the same number repeatedly. Their description says " The Math.floor() function returns the largest integer less than or equal to a given number," which doesn’t sound very “random” either.
Can you tell me more about generating random numbers with this function?

Math.floor has nothing to do with randomness, but it would still be useful here. At the moment, you’re generating numbers between 1 and 10 (inclusive). But your array indexes go from 0 to 9. So you never get the first element, and sometimes you get undefined when you’re trying to access array[10].

A common way around this is to use Math.floor(Math.random() * 10).

Thanks for the clarification. I’ve seen the undefined word a few times, and was thinking adding another word to the list might take care of that, since round rounds up for numbers > 0.5
I thought for a zero-based array 0-10 (or eleven elements) would take care of that.
I’ll do some more research on this. Thanks again!

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hey Ariel, thanks for the comments. If you have any sort of mail list, please put my name on it. I can use all the help I can get! Thanks,



If you haven’t signed up for Quincy’s weekly email, you should definitely do that!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.