Sunday, January 3, 2010

Using Math.random() with ArrayLists | ArrayList Vs Array

Math.random() is used to generate random numbers between a specified range.
   1: int rand = (int)(Math.random() * 5);
Here rand can be anything between 0 to 4.
But one faces the problem when he has to collect  random numbers in a set without repetition. Using Arrays is one of the ways to deal this situation. But then, every time a random number is generated, one need to check the whole array if it was initially picked up or not, which includes lots of complexity.
So picking the concept of ArrayList to deal this case is a better choice. ArrayList has a great property of removing the contents its holding even if they are in between many other members. It’s something like one leaving the human chain. Automatically rest of the chain is joined.

1 ----- 2 ----- 3 ----- 4 ----- 5 ----- 6 ----- 7 ----- 8 ----- 9 ----- 10

Here, if one removes 6 from the chain, rest of the numbers link back again.

1 ----- 2 ----- 3 ----- 4 ----- 5 ----- 7 ----- 8 ----- 9 ----- 10

Now one can use this idea for generating random numbers in a set without repetition.
Well I wrote a simple code for Housie game in which one has to generate random numbers between 1 and 100.

I created objects which indeed hold values, in this case numbers from 1 to 100. Go through the following codes.

This is Main.java

   1: package housie;
   2: import java.util.ArrayList;
   3:  
   4: public class Main {
   5:     public static void main(String[] args) {
   6:  
   7:         ArrayList<HousieNumber> board=new ArrayList<HousieNumber>();
   8:         int i;
   9:
  10:         for(i=0;i<100;i++){ // Inserting 100 numbers on the board
  11:  
  12:             board.add(i, new HousieNumber()); // Creating an object
  13:  
  14:             board.get(i).SetValue(i+1); 
  15:             // Now each object has its own value
  16:             //for the instance variable 'numb'
  17:         }
  18:  
  19:         int rand;
  20:         int drawnNumb;
  21:  
  22:         while(board.size()!=0){
  23:  
  24:             // Random number is generated within the range of 
  25:             //0 to size of ArrayList
  26:             rand = (int)(Math.random() * board.size());
  27:  
  28:             // Value of the corresponding object found 
  29:             //at index number od 'rand'
  30:             drawnNumb = board.get(rand).GetValue();
  31:  
  32:        System.out.println("Currenlt drawn number is: " + drawnNumb);
  33:  
  34:      // Removing the object at the index, 'rand'
  35:             board.remove(rand);
  36:         }
  37:     }
  38: }
This is HousieNumber.java

   1: package housie;
   2:  
   3: public class HousieNumber {
   4:  
   5:      private int numb;
   6:  
   7:     public void SetValue(int number){
   8:         this.numb=number;
   9:     }
  10:  
  11:     public int GetValue(){
  12:         return this.numb;
  13:     }
  14: }

4 comments:

  1. Mind blowing gr8 work and nice explanation :)

    ReplyDelete
  2. Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

    Hibernate Online Training Hibernate Online Training Spring Online Training Spring Online Training Spring Batch Training Online Spring Batch Training Online

    ReplyDelete
  3. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete