I have a question about random number generator code

var randomNum = Math.random();
var improvedNum = (randomNum * 3) + 1;
var finalNum = Math.floor(improvedNum);
alert(finalNum);

the first step seems unnecessary. Why not just start with step 2. Math.random is already its own function. No need to assign it to a variable.

The result of calling the function is saved as a variable, not the function itself.

what for? all we needed is Math.random() in step two.

Because the person who wrote the code wanted to make the separate steps clearer.

1 Like

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