I have a simple for loop that takes a string of characters and spits out 4 random ones, I use this to create game Id that is used in order to synchronize several users together. I feel like my method is not "random enough" and in some case can produce repetitive result even when character sting is that big. There is no need to store created game id's to compare against in order to see if they exist, I would, however, want to figure out a way to perform better random selection of characters from the string.
var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 4; i++ )
{
gameId += characters.charAt(Math.floor(Math.random() * characters.length));
}
Edit: perhaps shuffling string before for loop could be a way?
Aucun commentaire:
Enregistrer un commentaire