Tuesday, July 14, 2009

How to randomize elements of array?? (in C language)?

I need to put into array numbers from 0 to 15 but in random order... and every number should only be used only once... anybody has an idea how to do it???

How to randomize elements of array?? (in C language)?
Use an array to keep track of what is already present in the other array - or use an array to keep track of what isn't in the other array. Then just make sure you don't get duplicate random numbers (either by resizing the one array or using a while loop).
Reply:#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;time.h%26gt;





#define N 16





int main()


{





int i, randVal;


srand( (unsigned)time( NULL ) );





//Print 10 random values to screen


for (i = 0; i %26lt; 10; i++)


{


randVal = (int) N * rand() / (RAND_MAX + 1.0);


printf("%d\n", randVal);


}





return 0;


}








That code will generate and print random numbers between 0 and 15. All you would have to do then is initialize your array with lets says -1. Then maybe have another variable to keep track of how many slots have been filled. Call the random function until all slots are filled.....





Hopefully this helps and give you an idea of how to go about solving your problem.
Reply:Set two arrays: fill one with numbers from 0 to 15. set another filled with random numbers . Then sort the random column, but change the number os the first array accordingly:


assuming r[] has random numbers


n[] has the numbers


for i:=0 to 14 do


..for j:=i+1 to 15 do


....if r[i]%26gt;r[j] then


......begin


........temp:=r[i];


........r[i]:=r[j];


........r[j]:=temp;





........temp:=o[i];


........o[i]:=o[j];


........o[j]:=temp ;


......end;








BIG H proposed solution does not guarantee uniqueness





and prassanna solution does not gruarantee randomness, since change is done randomly.


Donald Knutt demonstrates that random algorhitms do not necessarilly produce random results.
Reply:Assume that you need the randomized elements in array 'a'. The code below should help you out:





#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;time.h%26gt;





#define NUM_ELEM16


#define MAX_ITER10000


#define swap(arr,pos1,pos2)\


arr[pos1] = arr[pos1] + arr[pos2]; \


arr[pos2] = arr[pos1] - arr[pos2]; \


arr[pos1] = arr[pos1] - arr[pos2];





void randomize_array ( int *a, int num_elem )


{


int i, j, k;





srand(time(NULL)); /* or any other random generator initialization call */


for (i = 0; i %26lt; num_elem; i++) {


a[i] = i;


}


for (i = 0; i %26lt; MAX_ITER; i++) {


j = random (num_elem);


/* on unix use: j = random() % num_elem; */


k = (j + 1 + random (num_elem - 1)) % num_elem;


/* on unix use:


* k = (j + 1 + random () % (num_elem - 1)) % num_elem;


*/


swap (a, j, k);


}


}





/* example main function. Written for the sake of completion */


int main (void)


{


int a[NUM_ELEM], i;





randomize_array (a, NUM_ELEM);


/* print the results */


for (i = 0; i %26lt; NUM_ELEM; i++) {


printf ("a[%2d] = %2d\n", i, a[i]);


}


}











Please let me know if you face any problems.





--


thanks and regards,


Prasanna


No comments:

Post a Comment