Here is a sample program that creates an array of a given size containing random values with the 'size' as the range.
The function 'createRandomArray' generates the random array.
RandomArray.java
//--------Start Of Source Code------
public class RandomArray
{
public static void main(String [] args)
{
int [] sample=createRandomArray(30);
for(int i=0;i<sample.length;i++)
{
System.out.println(sample[i]);
}
}
//This function performs all the randomization task
public static int[] createRandomArray(int size)
{
int [] randomArray=new int[size];
for(int i=0;i<size;i++)
{
randomArray[i]=i;
}
for(int i=0;i<size;i++)
{
int pos=(int)(size*Math.random());
int temp=randomArray[i];
randomArray[i]=randomArray[pos];
randomArray[pos]=temp;
}
return randomArray;
}
}
//------End Of Source Code-------
Happy Programming. ;)
Signing off.
Ryan
No comments:
Post a Comment