0

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?

2
Contributors
3
Replies
5
Views
4 Years
Discussion Span
Last Post by radhakrishna.p
0

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

Edited by radhakrishna.p: giving clarity on the solution

0

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

0

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

Edited by radhakrishna.p: providing clarity on the solution\

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.