Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

JavaScript RNG

Uploaded by

antei kuwute
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

JavaScript RNG

Uploaded by

antei kuwute
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

JavaScript RNG

//RANDOM NUMBER GENERATOR:

var n = Math.random(); /* you can use math method as a value in a variable because it's generating a
random number between 0-1.

This math random method will give you a 16 decimal place number and the number between 0-0.9
to 16 decimal places, it autimaticly give a number between 0-1 but never reaches 1*/

n = n * 6; /*we multiply the n value here, that's how we scale the random number, so the decimal
number that will sho up will be around 0-5.9 but never reches 6

and the result will get the math method automaticly, and the result will be decimal and it's because

the math random method will always give a random number with 16 decimal places*/

console.log(n);// the result is decimal.

//so, if you want to have a rounds number you just need to use math floor method:

n = Math.floor(n);

console.log(n); // the result will be a rounded number

//now if you want to make a dice here, you can use the math random method:

var dice = Math.floor(Math.random()*6 + 1); /*the dice never showing up a 0 right? so you have to
add 1 to get a number between 1-6.

id you didn't add the 1 the random generator will be giving a number from 0-5.99 and not giving a 6.
so if you add 1

the 0 will be 1 and the 5.99 will be 6.99, after that you can just use the math floor method so you
didn't get a decimal number*/

/*dice = Math.floor(dice);*/ //you can actually just put this floor method to the variable value and
input the math random method inside and also add 1.

console.log(dice);

//Now as an exercise we'll make a random love generator with rpompt and alert:

var name1 = prompt("Input your name here");

var name2 = prompt("Input your partner name here");

var lovePercentage = Math.floor(Math.random() * 100 + 1);


alert("Your love percentage is " + lovePercentage + "% with your partner");

console.log(lovePercentage);

You might also like