Java Program – Reshuffle the Objects in the List Randomly

Introduction

This Program helps to reshuffle the list of object randomly so the developer can get the different results on each and every execution of the Program.
It is very useful when the client wants some report which picks the data randomly.
We have implemented this algorithm using Java Programming Language but this is mandatory. The developers can write a program using any programming language. Also, we have some Database function which can we used to get the result from the database randomly. We will explain about Random using SQL in a different post.

Program Code

package com.kw.sample;

import java.util.ArrayList;
import java.util.List;

/**
 * This class helps to Reshuffle the list of object randomly.
 * 
 * @author dsahu1
 * 
 */
public class KWShuffleObjectRandomly {

	/**
	 * This method helps to start the program execution.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("Method to Reshuffle List of Object Randomly!");

		List<Long> objs = new ArrayList<Long>();
		// Call method to load the object into the list
		loadObjectInList(objs);
		// Call method to reShuffle the Object
		reShuffleObjectRandomly(objs);

	}

	/**
	 * This method helps to load the objct into the list for testing purpose.
	 * 
	 * @param objs
	 */
	private static void loadObjectInList(List<Long> objs) {

		for (int i = 0; i < 1000; i++) {
			objs.add((long) i);
		}
	}

	/**
	 * This method helps to Reshuffle the object in Random ways. It use Java
	 * Random API.
	 * 
	 * @param objs
	 */
	public static void reShuffleObjectRandomly(List<Long> objs) {

		int m = objs.size();
		int n = objs.size();

		int[] perm = new int[n];
		for (int i = 0; i < n; i++) {
			perm[i] = i;
		}

		for (int i = 0; i < m; i++) {
			int r = i + (int) (Math.random() * (n - i));

			int t = perm[r];
			perm[r] = perm[i];
			perm[i] = t;
		}
		StringBuilder strBuilder = new StringBuilder();
		// Print the result
		for (int i = 0; i < m; i++) {

			strBuilder.append(perm[i]);
			if (i < m - 1) {
				strBuilder.append(",");
			}
		}
		System.out.print(strBuilder);

	}

}

Program Version 1.1

package com.kw.sample;

import java.util.ArrayList;
import java.util.List;

/**
 * This class helps to Re-shuffle the list of object randomly.
 * 
 * @author dsahu1
 * 
 */
public class KWShuffleObjectRandomly {

	/**
	 * This method helps to start the program execution.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("Method to Reshuffle List of Object Randomly!");

		List<Long> objs = new ArrayList<Long>();
		// Call method to load the object into the list
		loadObjectInList(objs);
		// Call method to reShuffle the Object
		reShuffleObjectRandomly(objs);

	}

	/**
	 * This method helps to load the object into the list for testing purpose.
	 * 
	 * @param objs
	 */
	private static void loadObjectInList(List<Long> objs) {

		for (int i = 0; i < 1000; i++) {
			objs.add((long) i);
		}
	}

	/**
	 * This method helps to Re-shuffle the object in Random ways. It use Java
	 * Random API.
	 * 
	 * @param objs
	 */
	public static void reShuffleObjectRandomly(List<Long> objs) {

		int m = objs.size();
		int n = objs.size();

		List<Long> reSuffledobjs = new ArrayList<Long>();

		System.out.println("ORIGINAL LIST:");
		System.out.println(objs);
		int[] perm = new int[n];
		for (int i = 0; i < n; i++) {
			perm[i] = i;
		}

		for (int i = 0; i < m; i++) {
			int r = i + (int) (Math.random() * (n - i));

			int t = perm[r];
			perm[r] = perm[i];
			perm[i] = t;
		}
		// Print the result
		for (int i = 0; i < m; i++) {
			reSuffledobjs.add((long) perm[i]);

		}
		System.out.println("UPDATED LIST After Re-SUFFLE:");
		System.out.print(reSuffledobjs);

	}

}

Output

Run1:

Method to Reshuffle List of Object Randomly!
429,239,296,684,21,394,772,89,117,926,446,921,643,533,919,851,232,962,768,308,310,480,211,487,20,81,867,167,896,980,126,563,249,57,65,452,453,844,663,54,858,430,923,15,752,989,784,70,363,290,940,890,793,136,316,350,481,620,636,143,823,137,713,366,2,412,375,709,882,767,605,596,47,26,184,370,437,408,119,904,514,912,527,671,873,40,705,759,539,532,484,951,918,252,124,473,424,415,224,206,275,834,36,235,613,856,963,578,934,504,571,379,831,200,44,114,127,34,587,149,469,386,544,996,177,187,100,56,993,637,312,321,796,557,173,593,417,809,188,540,499,160,723,510,786,340,307,440,617,872,496,659,60,24,435,241,976,591,552,575,191,678,604,880,732,929,37,503,138,528,777,628,531,866,107,538,987,522,66,565,968,175,715,694,662,536,271,549,251,439,598,418,233,670,197,243,48,289,810,365,413,672,396,579,250,876,550,501,828,448,868,18,584,199,988,262,859,467,871,609,751,749,401,815,737,330,548,267,611,545,599,380,278,227,470,456,359,765,837,427,658,268,703,230,917,879,508,845,836,407,328,441,509,244,185,698,189,129,697,668,652,344,283,817,305,105,885,848,932,621,111,269,722,700,113,573,69,645,992,236,537,909,937,781,38,75,372,716,895,303,893,246,526,607,423,214,131,3,974,369,201,821,120,649,291,4,238,357,502,218,874,574,760,367,869,681,654,707,29,297,311,758,789,14,601,818,924,675,64,31,492,300,564,799,50,750,147,186,920,314,805,434,998,351,28,171,516,302,345,901,965,403,977,969,822,807,666,68,986,733,592,756,490,335,377,112,648,506,755,775,183,955,135,512,863,299,560,356,555,123,908,729,358,455,213,88,9,195,634,855,486,597,134,71,438,237,444,491,447,436,61,261,58,207,460,953,776,221,952,978,602,825,556,280,741,546,406,984,288,217,646,861,122,193,202,711,245,990,301,7,266,295,464,906,744,157,170,228,142,727,877,757,650,554,298,293,400,695,132,257,806,813,67,631,712,900,773,913,154,606,814,559,420,389,674,212,254,468,55,96,761,710,979,144,656,726,277,829,155,449,393,104,914,151,159,85,770,925,110,49,894,72,852,581,361,570,928,402,740,397,83,414,922,961,248,320,485,282,158,610,19,82,45,164,62,615,395,477,74,355,139,43,128,586,169,792,864,771,967,661,735,685,471,714,791,272,816,5,425,785,551,466,371,391,95,950,731,857,33,148,562,724,774,518,820,220,959,619,421,63,130,203,405,16,474,603,676,465,808,688,433,639,23,801,270,590,489,368,568,274,17,931,991,463,404,686,939,384,94,720,832,567,180,292,846,975,566,319,458,833,181,327,728,382,86,618,42,454,664,870,432,347,766,717,632,140,176,325,318,669,410,862,561,505,497,247,378,721,898,242,943,594,192,255,353,970,90,364,13,843,839,317,52,146,332,41,911,166,198,0,572,93,954,152,59,190,411,630,999,222,168,324,583,945,462,196,53,739,259,517,419,125,691,77,25,478,748,542,443,208,747,240,994,118,313,725,736,692,704,558,10,541,938,655,263,589,633,985,145,701,616,699,385,1,665,286,109,800,941,644,523,687,878,215,32,30,515,973,689,92,947,657,76,398,780,960,595,273,887,782,995,830,172,338,457,442,422,730,627,103,507,754,210,336,390,743,461,891,734,708,182,933,738,883,719,229,653,226,547,641,11,521,494,892,376,903,150,788,511,256,381,354,451,445,827,588,194,794,323,343,179,997,889,957,779,982,949,6,334,966,797,98,294,935,802,426,495,51,352,783,983,530,787,265,362,22,629,803,162,614,260,106,78,625,204,811,580,971,409,683,905,279,640,535,930,622,956,174,115,706,946,612,322,163,624,902,958,804,153,309,881,623,529,12,99,459,582,944,964,647,718,907,899,696,673,764,608,790,223,108,326,231,216,79,847,888,428,346,849,884,840,482,690,383,342,387,948,374,865,927,577,39,981,824,812,519,600,472,304,626,178,205,373,219,576,910,693,339,842,795,778,337,329,493,27,360,161,826,543,942,850,156,475,333,742,8,660,897,769,287,209,838,476,853,638,141,763,642,349,116,520,667,819,553,488,677,35,635,841,80,264,431,916,498,284,225,165,450,46,341,253,87,525,315,585,513,936,479,392,682,854,524,84,534,886,97,276,679,835,972,651,500,745,101,483,860,285,680,762,399,102,133,416,388,702,348,915,258,121,753,569,875,91,234,331,798,73,281,746,306

Leave a Reply

Your email address will not be published. Required fields are marked *