Wednesday, December 23, 2009

Forward

Coding it forward. In other words, I'm here to share what I've likely learned from others in this world of ones and zeros. I often find myself scouring the internet for solutions to problems that seem very complicated, but in the end the solution usually turns out to be very simple and straight forward.

This is of course after much frustration, lots of coffee, temper tantrums, and sometimes cursing which has probably caused my coworkers enough concern to arm themselves in preparation for cubicle defense. The solution is usually arrived at by assembling bits and pieces from several sources until it all becomes clear. Other times, it is just a matter of wading through thousands of bad solutions until the cleanest one is found.

Sometimes it even makes sense. Sometimes I don't understand it completely, but I do know that it works.

I like solving problems; it is why I'm in the IT field, just like many of you probably are. If you don't find the solution you're looking for feel free to ask me - post a question, make me work for your interest. Most of my posts will revolve around solutions in Java, JBoss, MySQL, Oracle, and Linux. These are the tools I'm currently the most active in, but I have a diverse exposure to many other technologies, such as ASP.NET, C#, perl, PHP, VB, PowerBuilder, etc... the list goes on.

So, my goal with this blog is to assemble these nuggets of genius (okay, I'm using that term very loosely), and "pay it forward", by presenting them for any of you frustrated geeks out there seeking answers with the infamous google search engine hoping to find those magic keywords that throw the solution back at you.

To christen this blog, I'm going to start it with a simple classic algorithm for randomizing a collection. I like to refer to it as the "shuffle" algorithm. I'll be doing the "HOW TO" example in Java, but you could easily transfer this same technique to any language. If you'd like it presented in another language feel free to post a comment, and if I'm capable I'll try to provide an example.


package com.blogspot.codingitforward;

import java.util.Random;

public class ShuffleExample {
public ShuffleExample() {
//Creating a sample int array
int[] shuffledArray = new int[100];

//setting up some values to randomize
for(int ndx = 0; ndx < shuffledArray.length; ndx++) {
shuffledArray[ndx] = ndx + 1;
}

//Initialize your random number generator
Random random = new Random();

//loop through the array, and "shuffle" the items by swapping them.
//some items will be swapped several times, but you can be assurred that
//every element will be moved at least once. This allows you to very efficiently
//randomize a set, and also keep the set finite.
for(int ndx = 0; ndx < shuffledArray.length; ndx++) {
//grab a random number between 0, and the size of your collection
int iRand = random.nextInt(shuffledArray.length);

//put the current value in a temporary holder
int iTemp = shuffledArray[ndx];

//set the value for your current index value to the random element
shuffledArray[ndx] = shuffledArray[iRand];

//set the value for the random element to what WAS the current one
shuffledArray[iRand] = iTemp;
}

//For grins, lets output the random results
for(int ndx = 0; ndx < shuffledArray.length; ndx++) {
System.out.println("Element " + ndx + " = " + shuffledArray[ndx]);
}
}

public static void main(String[] args) {
ShuffleExample shuffleExample = new ShuffleExample();
}
}


Stay tuned. I may provide an extended example which utilizes this technique to update database records with random values.

I know, it is quite riveting. I bet you're on the edge of your seat right now.