lundi 23 novembre 2020

How to get truly random numbers when using javaScript Math.random() [closed]

I was working on a little widget that generates 2D characters and I was wanting to implement a button that randomizes everything.

I have a bunch of images ending in integers 1,2,3,..8,etc.

image folder

image folder

and I was wanting to call the Math.random() function to generate a random integer between 1 and the n number of clothing options for that category. However, when I load the page and generate the character, I get the same "random" character every time, even after exiting and reloading everything. I have tried a few different options, with no luck. Any tips would be great.

function getRandomArbitrary(min, max) {
  min = Math.ceil(min);
  min = Math.floor(max);
  
  return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}

function resetCharacter(){
  hairName = "hair1";
  headName = "head1";
  torsoName = "torso1";
  legsName = "legs1";
  lArmsName = "leftArm1";
  rArmsName = "rightArm1";
}


function randomizeCharacter(){

  hairName = "hair" + getRandomArbitrary(1, 6);
  headName = "head" + getRandomArbitrary(1, 5);
  torsoName = "torso" + getRandomArbitrary(1, 9);
  legsName = "legs" + getRandomArbitrary(1, 5);
  var armNumber = getRandomArbitrary(1, 5);
  lArmsName = "leftArm"+armNumber;
  rArmsName = "leftArm"+armNumber;
}



Aucun commentaire:

Enregistrer un commentaire