create array with random numbers code example

Example 1: javascript generate array of random different numbers

var arr = [];
while(arr.length < 8){
    var r = Math.floor(Math.random() * 100) + 1;
    if(arr.indexOf(r) === -1) arr.push(r);
}
console.log(arr);

Example 2: generate random number array javascript from principal array

let numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20],
    generated = [];
for (let i = 0; i < 10; i++)
{
    let random_index;
    while(!random_index)
    {
        let tmp = Math.floor(Math.random() * numbers.length);        
        if( !generated.filter( (g) => numbers[tmp] == g).length )
            random_index = tmp;
    }
    generated.push(numbers[random_index]);
}
console.log(generated);

Example 3: generate an array of random numbers javascript

let numbersArray = [] , max = 100;

for( var i=1; numbersArray.push(i++) < max;);  // numbers = [1,2,3 ... 100]

Example 4: assigning an array with random numbers

public static double[] list() {
    anArray = new double[10];
    for(int i=0;i<anArray.length;i++)
    {
        anArray[i] = randomFill();
    }
    return anArray;
}

Tags:

Misc Example