function makeid(length) {
var result = [];
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result.push(characters.charAt(Math.floor(Math.random() *
charactersLength)));
}
return result.join('');
}
console.log(makeid(5)); //Generate random string/characters.
var random;
var max = 8
function findRandom() {
random = Math.floor(Math.random() * max) //Finds number between 0 - max
console.log(random)
}
findRandom()
/*
The Math.random() function returns a floating-point, pseudo-random
number in the range 0 to less than 1 (inclusive of 0, but not 1)
with approximately uniform distribution over that range — which you
can then scale to your desired range. The implementation selects the
initial seed to the random number generation algorithm; it cannot
be chosen or reset by the user.
*/
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
console.log(getRandomInt(3));
// expected output: 0, 1 or 2
console.log(getRandomInt(1));
// expected output: 0
console.log(Math.random());
// expected output: a number from 0 to <1
Math.random()
// Or something between 0 and 9:
Math.floor(Math.random() * 10)
// You can even make functions:
function random(min, max){return Math.floor(Math.random() * (max - min)) + min}
/*
`Math.random` returns a pseudo-random number between 0 and 1.
a pseudo-random number is generated by an algorithm, it is not
technically actually random, but for all intents and purposes
it is random enough that no human should be able to find a
pattern
*/
Math.random(); // -> Decimal number between 0 and 1
Math.round(Math.random()); // -> 0 or 1
Math.random() * max; // -> Decimal number between 0 and max
Math.floor(Math.random() * max); // -> Whole number between 0 and max - 1
Math.round(Math.random() * max); // -> Whole number between 0 and max
Math.ceil(Math.random() * max); // -> Whole number between 1 and max
(Math.random() * (max - min)) + min; // Decimal number between min and max
Math.floor((Math.random() * (max - min)) + min); // Whole number between min and max - 1
Math.round((Math.random() * (max - min)) + min); // Whole number between min and max
Math.ceil((Math.random() * (max - min)) + min); // Whole number between min + 1 and max
Math.random() * Math.random(); // Decimal number between 0 and 1 with a tendency to be smaller
1 - Math.random() * Math.random(); // Decimal number between 0 and 1 with a tendency to be larger
/*
To generate a random number between a and b keep in mind this formula:
a-> minimum value
b->maximum value */
let randomNumber = a+(b-a)*Math.random();
/*this gives you any random number between a and b
*/
var random = Math.floor (Math.random() * 10) + 1;
//This generates a number between 10 and 1
example.children[random].innerHTML = random;
//This displays the number on screen
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; // max & min both included
}
// You can make functions aswell
function randomNum(min, max) {
return Math.floor(Math.random() * (max - min)) + min; // You can remove the Math.floor if you don't want it to be an integer
}
var options = ["Your options", "Another option!", "This is an option."];
var chosenOption = Math.floor(Math.random() * options.length);
console.log(options[option]);
Math.floor(Math.random() * 10) + 1 // Random number Between 1 and 10
// First Math.random give us a random number between 0 and 0,99999
// The we multiply it by 10
// And we round dow with Math.floor
// We add 1 so the result will never be 0
// Another Example:
h.floor(Math.random() * 20) + 10 // Random number Between 10 and 20
//returns a random number between 1 and maxNumber
//replace Math.ceil with Math.round if you want to include 0 as well
const randomNumber = (maxNumber) => {
return Math.ceil(Math.random() * maxNumber);
}
// Retorna un número aleatorio entre min (incluido) y max (excluido)
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
// using array and random example
function getTheBill() {
let names = ["bob", "mike", "matt"];
let randomFriend = Math.floor(Math.random() * names.length);
let randomFriendBuyng = names[randomFriend];
return randomFriendBuyng + " is buying us lunch today!";
}
var rand = "_" + Math.random().toString(36).substr(2, 9);
console.log(rand)
// or use this
var e = Math.random().toString(31).substring(2)
console.log(e)