#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    24
    Rep Power
    0

    rand() to generate ARRAY of numbers


    Hi all,

    I need to generate an array of random integers. The number of integers can vary depending on some formula. The most important thing is that the integers MUST be unique. I can't have duplicate numbers occurring throughout the array. How can I go about this? At the moment, I just have a for loop that iterates as many times are there is space in the array, and I call the "rand" function each time, like:

    Code:
      array[i] = (int)( ( ( (double)rand() )/RAND_MAX)*10); 
      //I want the range to be from 0 to 10
      //i is the for loop variable
    Obviously, this does not take care of UNIQUENESS.
    Is there any function that can generate an ARRAY instead which contains UNIQUE integers???

    THanks!
    Kavi
  2. #2
  3. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    one way would be to use two arrays. you say the random number is going to be between 1 and 10? so that's a maximum of 9 possible unique number then.

    build two arrays, one with the number of elements in your required range which is 9 in your case i think. fill the array using a for loop in a sequential non-random way with the actual numbers - from 1 to 9.

    make another array with the same number of elements, 9. do another for loop but this time fill each element with an entierely random value - don't even bother restricting or confining it to a particular range, so however the random numbers come - doesn't matter.

    then using some sort of sort, like qsort, sort the random array numerically, and in the same way, at the same time in parallel sort the other array. the other array will end up with all your numbers once and only once, in a random order.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    24
    Rep Power
    0
    You are a genius! Thanks for your wonderful idea! I just tested it, and it's totally cool :)

    Thanks!!
  6. #4
  7. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    If you want the range to be from 0..10, then that is 11 unique numbers, not 9. ;)

    I think a better way of solving this is like so (if, in fact, you plan on having exactly the same number of elements in the array as the maximum possible unique numbers, which seems to be the case):

    Use only one array, of 11 elements. Fill the elements incrementally 0, 1, 2, ... 10 so that the index = the value within the index.

    Now, randomly sort the array. How do you do this to make sure every index gets sorted? Swap elements exactly 11 times - the first index is the loop counter (0..10, incrementally), and the second index is random.

    Here's the source, in case you didn't follow:

    Code:
    #include <stdio.h> // printf()
    #include <stdlib.h> // rand()
    #include <time.h> // time()
    
    int main()
    {
    	// declaration
    	int array[11]; // elements 0..10
    	int i;
    
    	// step 1: initialize the array
    	for (i=0; i<=10; i++)
    		array[i] = i;
    
    	// step 2: sort the array
    	srand( (unsigned)time( NULL ) ); // randomize the rand() function
    	for (i=0; i<=10; i++)
    	{
    		int index1 = i;
    		int index2 = rand()%11; // x % 11 = 0..10
    
    		// swap elements index1 and index2
    		int temp;
    		temp = array[index1];
    		array[index1] = array[index2];
    		array[index2] = temp;
    	}
    
    	// show the results.
    	for (i=0; i<=10; i++)
    		printf("array[%d] = %d\n",i,array[i]);
    
    	return(0);
    }
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    24
    Rep Power
    0
    Hey Jason

    That helped a lot too....Thank you very much!! Appreciate it :)

    Kavi
  10. #6
  11. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    ! i have no idea why i got 9! i was reading about sets and unions etc and wasn't understanding what i was reading just previous to seeing this question, so i blame my confusion on that :) but then it is actually 10 not 11! (1..10) doesn't matter though.

    that is definetely a better way as it doesn't use a 2nd array, so i guess it must be more efficient.

    and the array that you fill sequentially then sort randomly - that could contain anything - pointers, characters, other ints, whatever. not necessarily values that reflect the index.

IMN logo majestic logo threadwatch logo seochat tools logo