How can I get random values from an array until the elements of it end?

4

The fact is that I want to get a random array values from a array until they end.

All the array have 7 values in total and, in the end, what I want to achieve is to obtain a combination of these values automatically to generate sentences from it mode.

What I want is for these values colors, countrys and days to be sorted randomly.

This is the loop used for what it's worth:

var colours = ["White", "Red", "Black", "Purple", "Grey", "Yellow", "Blue"];
var countrys = ["Japan", "Korea", "Spain", "England", "China", "Singapur","Rumania"];
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; 
    
for (var n = 0; n < colours.length; n++) {
        document.write("My " + (n+1) + " choice is " + colours[n] + " in " + countrys[n] + " on " + days[n] + "<br>");
}
    
asked by Kelvin Macay 09.01.2018 в 18:34
source

4 answers

4

You can determine that when making the loop take random elements of each array, for example:

colours[Math.floor(Math.random() * colours.length)] , you would get a random element from the array colours .

This would be a complete example:

var colours = ["White", "Red", "Black", "Purple", "Grey", "Yellow", "Blue"];
var countrys = ["Japan", "Korea", "Spain", "England", "China", "Singapur","Rumania"];
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

/*for (var n = 0; n < colours.length; n++) {
    document.write("My " + (n+1) + " choice is " + colours[n] + " in " + countrys[n] + " on " + days[n] + "<br>");
}*/


for (var n = 0; n < colours.length; n++) {
    document.write("My " + (n+1) + " choice is " + colours[Math.floor(Math.random() * colours.length)] + " in " + countrys[Math.floor(Math.random() * countrys.length)] + " on " + days[Math.floor(Math.random() * days.length)] + "<br>");
}
    
answered by 09.01.2018 / 18:44
source
2

Instead of selecting the options randomly and checking that they do not repeat, you could arrange the arrays randomly ( eg: shuffle )

For this you could use the version of Fisher–Yates shuffle algorithm .

Demo:

function shuffle(array) {
  var m = array.length,
    t, i;

  // While there remain elements to shuffle...
  while (m) {

    // Pick a remaining element…
    i = Math.floor(Math.random() * m--);

    // And swap it with the current element.
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }
  return array;
}

var colours = shuffle(["White", "Red", "Black", "Purple", "Grey", "Yellow", "Blue"]);
var countrys = shuffle(["Japan", "Korea", "Spain", "England", "China", "Singapur","Rumania"]);
var days = shuffle(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]);

for (var n = 0; n < colours.length; n++) {
    document.write("My " + (n+1) + " choice is " + colours[n] + " in " + countrys[n] + " on " + days[n] + "<br>");
}
    
answered by 09.01.2018 в 18:50
2

You could create a method that randomizes the arrays and then display them as you are doing.

let colours = ["White", "Red", "Black", "Purple", "Grey", "Yellow", "Blue"];
let countrys = ["Japan", "Korea", "Spain", "England", "China", "Singapur","Rumania"];
let days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; 

function shuffle(array) {
      var j, x, i;
      //Recorremos el array del final hacia delante
      for (i = array.length - 1; i > 0; i--) {
        //Generamos una posicion comprendida entre los valores de nuestro array
        j = Math.floor(Math.random() * (i + 1));
        // Asignamos el valor de la posición actual a una variable
        x = array[i];
        //Intercambiamos los valores de las dos posiciones
        array[i] = array[j];
        array[j] = x;
      }
    }
    
    shuffle(colours);
    shuffle(countrys);
    shuffle(days);
    
    for (var n = 0; n < colours.length; n++) {
        document.write("My " + (n+1) + " choice is " + colours[n] + " in " + countrys[n] + " on " + days[n] + "<br>");
    }

This way you make sure you get a combination of all values, without repeating.

    
answered by 09.01.2018 в 18:54
1

Another example using prototype

Array.prototype.MostrarColor = function(){
       let i = Math.floor(Math.random() * (colours.length ));
       let Mycolor = colours[i];
        colours.splice(i,1);
        return Mycolor;
}    
Array.prototype.MostrarDia = function(){
       let i = Math.floor(Math.random() * (days.length ) );
       let MyDay = days[i];
        days.splice(i,1);
        return MyDay;
}    
       


let colours = ["White", "Red", "Black", "Purple", "Grey", "Yellow", "Blue"];
let countrys = ["Japan", "Korea", "Spain", "England", "China", "Singapur","Rumania"];
let days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; 


for (var n = 0; n < countrys.length; n++) {
    document.write("My " + (n+1) + " choice is " + countrys.MostrarColor() + " in " + countrys[n] + " on " + countrys.MostrarDia() + "<br>");
}
    
answered by 09.01.2018 в 19:26