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.

4 Comments:

Blogger Vaisagh said...

of course .... just talked to a certain junior before posting this. that was my inspiration..

9:59 PM

 
Anonymous Anonymous said...

Great ,
I was thinking about how much
random is the random no generated
by a computer.

Exactly how does it generate a random element ?That is how do we define randomness?Quantify randomness?

Then one more thing I think there's some
method to calculate the efficiency of
your algorithm .Check out that .

Worst case method says ,I feel ,that as
the array size becomes very large time
of calc increases-what if all the random nos generated are already occupied?Then
the random no generation will have to be
done as many times as the position you are
in ,at that moment .So it may be a problem.
I dont know ,but may be the randomness prevents such a case from occuring very often.

mkb

10:11 PM

 
Blogger Vaisagh said...

i've no idea as to how it generates a random number. but the other problem about the same number being generated again and again does occur... for example in the su do ku programme i did... it will eventually get a result but that 'll destroy the whole purpose of quick generation(it might even take more than a minute ot two). what i did was. if it generates the samw place about , say 25 or fifty times, start all over again(use GOTO or something like that)... and due to the 'randomness' , you get the answer almost immediately.

10:28 PM

 
Anonymous Anonymous said...

elaneer vellam personally thinks this stuff belongs to a BORINGGG scientific journal and not a blog but in order not to insult it's sensitive owner, elaneer vellam says this instead:
*Clap* *Clap* *Clap*
Although I dont have the slightest clue what the hell you have been writing! :)

8:56 AM

 

Post a Comment

<< Home