Snippets Collections
https://source.unsplash.com/user/c_v_r
const randomID = new Date().getTime().toString().substring(3, 10)
const names = ['david', 'gary', 'shaun', 'stephen'];


function getRandomIndex(array){
  const index = Math.floor(Math.random() * array.length);
  let randomNumber = array[index];
  return randomNumber;
}

getRandomIndex(names)
double[] a = new double[n];
for (int i = 0; i <n; i++){
  a[i] = Math.random();
}
/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive).
 * The value is no lower than min (or the next integer greater than min
 * if min isn't an integer) and no greater than max (or the next integer
 * lower than max if max isn't an integer).
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
let clickMe = document.querySelector('button');

function getRandomNumber(min, max) {
  let totalEle = max - min + 1;
  let result = Math.floor(Math.random() * totalEle) + min;
  return result;
}
function createArrayOfNumber(start, end) {
  let myArray = [];
  for (let i = start; i <= end; i++) {
    myArray.push(i);
  }
  return myArray;
}
let numbersArray = createArrayOfNumber(1, 10);

clickMe.addEventListener('click', () => {
  if (numbersArray.length === 0) {
    console.log('No more random number');
    return;
  }
  let randomIndex = getRandomNumber(0, numbersArray.length - 1);
  let randomNumber = numbersArray[randomIndex];
  numbersArray.splice(randomIndex, 1);
  console.log(randomNumber);
});
star

Tue Apr 09 2024 01:29:12 GMT+0000 (Coordinated Universal Time)

#random #image #url
star

Tue Nov 07 2023 00:13:25 GMT+0000 (Coordinated Universal Time)

#javascript #random #number
star

Sun Mar 19 2023 07:48:30 GMT+0000 (Coordinated Universal Time)

#index #random
star

Wed Jan 26 2022 23:53:43 GMT+0000 (Coordinated Universal Time)

#random #number
star

Mon Dec 20 2021 13:47:33 GMT+0000 (Coordinated Universal Time)

#javascript #random #min #max
star

Fri Apr 16 2021 07:42:52 GMT+0000 (Coordinated Universal Time) https://playcode.io/757523/

#javascript #array #random #randomarray #dom

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension