Hello friends,

Need help in generating random numbers from an array without repeats.Once a number is generated, the number has to be deleted from the array so that only the remaining numbers can be generated from the array the next time we call that function.And it should go on until all the numbers from the array are deleted.


The code i have developed is below

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>

int random ()
{
    srand (time (NULL));

    int picked[8], i;
    for (i = 0; i < 9; i++)
         picked[i] = 0;
    
    int array[9];  
    int value;
     
    for (i = 0; i < 9; i++)
    {
        value = rand () % 9;
        if (picked[value])
            i--;  // already picked.  for-loop increments, so decrement here
        else
        {
            array[i] = value;
            picked[value] = 1; // hasn't been picked yet.  Assign to array,
                                // flag as picked.
        }
     }
     
     // display
     for (i = 0; i < 9; i++)
        printf("Values in the array are %d\n", array[i]);   
       getch();
      return value; 
}
int main()
{
     int s;
    s = random();

    printf("value=%d", s);
    getch();
    
}

Hope replies asap from you friends.

Recommended Answers

All 3 Replies

This code:

picked[value] = 1; // hasn't been picked yet.  Assign to array,
                                // flag as picked.

won't work unless you defined picked with enough space for all the random integers possible (RAND_MAX). This of course is not a great idea because you would waste an awful lot of space. Try setting each array element to the random number generated. To check if it has already been picked, loop through the array and see if there are any matches to the number generated.

Apart from the heavy memory consumption problem stated by "death_o_clock" which need to be corrected,your program has other major errors.

1> picked[8] can carry flags only up to integer 7 and

for (i = 0; i < 9; i++)
         picked[i] = 0;

will itself give error.Therefore picked[9] is the declaration you should make for your idea to work.

2> Other major flaw is in code

for (i = 0; i < 9; i++)
    {
        value = rand () % 9;
        if (picked[value])
            i--;  // already picked.  for-loop increments, so decrement here
        else
        {
            array[i] = value;
            picked[value] = 1; // hasn't been picked yet.  Assign to array,
                                // flag as picked.
        }
     }
     
     // display
     for (i = 0; i < 9; i++)
        printf("Values in the array are %d\n", array[i]);   
       getch();
      return value;

You have taken so much time to generate 9 random numbers 0-8 ( rand() % 9 )such that if a number repeats then it is not considered and again a number is generated a random.
After all this effort you are just sending the random number which is generated at last back to main.
This is just as calling :

for(i=0;i<9;i++)
       s=rand()%9;

in main itself.Ultimately you are just getting the random value generated on the ninth looping.

This question has been asked many times. There's a few ways to do this, an easy way is to simply fill up the array with unique integers, then shuffle it like this:

#include <stdio.h>

int main() {
  int arr[10];
  
  int i;
  for (i = 0; i < 10; ++i)
    arr[i] = i;

  for (i = 0; i < 10; ++i) {
    int *a = &arr[ i ];
    int *b = &arr[ rand() % 10 ];
    
    int temp = *a;
    *a = *b;
    *b = temp;
  }

  for (i = 0; i < 10; ++i)
    printf( "%d ", arr[i] );
}

Hope this helps.

commented: Yes, shuffling sounds good. +28
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.