Drawing of numbers with exception

Asked

Viewed 1,180 times

2

How do I draw lots n of numbers in C language where I can exclude the possibility of drawing a certain number from my range given a certain condition?

Exemplifying in code:

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

int main(void)
{
     int i,;
     int x[8];

     printf("Gerando 10 valores aleatorios:\n\n");
     for(i=0;i<8;i++){
        scanf("%d", &x[i]);
        if(x[i]==3){
                x[i] = rand()% 8 ("com exceção do i");
            }
        }
     }

     return 0;
}
  • 2

    What are you going to do with these numbers? Put them in an array? Or will generate them within one for or while and consume them during iteration without needing to store them for later use?

  • @Victorstafusa I have a vector of 8 positions, and I want the program to do the draw from 0 to 8. And it will have a condition that will exclude certain number from this draw. Example: if (counter==0){ don’t put 3 in the draw}. Type this.

  • Edit the question to make it clearer. I don’t get it right. What’s in this vector? Do you want to fill 8 random numbers from 0 to 8 in the vector, specifying a certain number? Or do you want to choose a vector position for some reason? Or do you just want to shuffle the vector?

  • @Victorstafusa edited the question.

  • That is, if the user type 3 is to put a random number from 0 to 7 in the array. Otherwise it is to put the number that the user type?

  • That’s exactly it, when he type 3 at position 5 of the vector, the program has to replace it with a random number other than 5.

  • Thanks for the help to all!!!

Show 2 more comments

2 answers

3


Following the same reasoning of colleague Maniero, I suggest that you repeat the sampling until you get a valid value. See the code below. Note: one should use srand to initialize the sample to obtain different values each time the program is run.

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

int main(void) {
    int x[8];

    srand(time(NULL));
    printf("Gerando 10 valores aleatorios:\n");
    for (int i = 0; i < 8; i++) {
        scanf("%d", &x[i]);
        if (x[i] == 3) {
            int sorteado = rand() % 8;
            while (sorteado == i){
                printf("recusado: %d ",sorteado);
                sorteado = rand() % 8;
            }
            printf("sorteado = %d \n",sorteado);    
            x[i] = sorteado;
        }
        printf("[%d] = %d\n", i, x[i]);
    }
}

2

You need to draw the number until you don’t get one that you don’t care about. There are other solutions, but for a simple cases so there is no need to complicate. Would that:

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

int main(void) {
    int x[8];
    printf("Gerando 10 valores aleatorios:\n");
    for (int i = 0; i < 8; i++) {
        scanf("%d", &x[i]);
        if (x[i] == 3) {
            int sorteado = -1;
            while ((sorteado = rand() % 8) != i); //repete até achar um valor aceitável
            x[i] = sorteado;
        } else {
            x[i] = rand() % 8;
        }
        printf("[%d] = %d\n", i, x[i]);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.