Wednesday, September 27, 2006

Randomizing 2D arrays

Hope that sounds technical and random enough for me to begin my blog. Actually i'm just a student of computer engineering first year... but iwa s pretty intersted in programming and did a lot of it in my eleventh and twelfth. One of the most interesting problems i ever did was randomizing a 2D array. If you don't even know what an array is go no further.. this is not for you...
if you are a software engineer or an expert in programming please don't be too harsh on me while commenting...:-)
When you want to randomly generate a 1 dimensional array which has no digit or character repeated. the easiest way would be to just make a for loop . generate a random number and then check if that number has appeared before the current spot. if it has... generate another number..else go to the next posittion in the number.........All well and good
But now... what if we have to randomize a 2D array.. as far as i know there are atleast four ways-
1) take pen and paper start writing it down.... this method is fast for small matrices but for larger numbers it becomes tiring :-)
2) generate two 1D arrays(as done above) and combine them(yep... really simplebut crude,wierd, and slow ).I actually haven't tried it but i know that a typical 4*2 matrix would take close to 30-50 seconds atleast to load(in C++)
3)generate the 2D matrix as in the method above(for 1D arrays)... ti's different from the first in the fact that only one 2D array is being generated and traversed on(if you know what i mean!!!)
this would be faster than the two above but still would take a long time to load(atleast 10 seconds )
4) The last and most interesting one is my method (naturally!!!).. you can actually use this for 1D 2D or even 3D arrays (yep!...its possible.. i used it to make a sudoku generator) and that too instantly...Here goes.
the basic trick is instead of traversing an array and generating a random number for each position.... you can actually choose one number and randomly select a location for it to be entered... get what i mean???
For this we would need two identical matrices...the anolgy would be that we have a table or matrix..which is initially blank(1st matrix) and all the postions are covered with a mark or something(try it out with a paper and some tokens)(2nd matrix). and we have a box of the numbers which we have to put in this table.
firstly we take one number from the box and generate a random position..... if this is covered.... we take another position.... and check if its occupied if tis not we put the number there. then we take the next number from the box and continue....

try and write the code for this.... if you really want this program... just comment...

The two reasons for this being fast are-
1) there is no needless traversing of the array each time a new number is generated(to check if that number already exists)
2) it's a computer for goodness sake!!!

you can generate almost any large array and even my old PC gives me the result instantly.