I need to generate a pattern contain 3 numbers , 1 '+' or '-' and '=' in any order. for eg: 2 3 + = 5, - 4 3 = 1 .....

How can i do this?

try like this, it may help you for your requirement

steps :

1 . store your numbers and operators in an array like as

var my_array = [1,2,3,.....]

2 . generate a random number between zero and the array length

var min =0 ,max = my_array.length // length of the given array as above
var x = Math.floor(Math.random() * (max - min) + min);

3 . based on the random number pick the value from the array

alert (my_array[x]);  // here i used alert for shoing the value 

go ahead like this

happy coding

I have an array of 20. and I randomnly fill 2 position with 1 and the remaining with 0. how to do it?

generate 2 random positions as follows or shown above

var min =0 ,max = your_array.length // length of the given array as above
var x = Math.floor(Math.random() * (max - min) + min);

and store those 2 positions into some variables like (pos1,pos2)

then loop thru the array and repalce the values accordingly as shown bellow

for(var i=0;i<your_array.length;i++){
    if(pos1 == i || pos2 == i){
        your_array[i] = 1;
    }else{
        your_array[i] = 0;  
    }
}

thats it

let me know the status

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.