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;
}
What are you going to do with these numbers? Put them in an array? Or will generate them within one
for
orwhile
and consume them during iteration without needing to store them for later use?– Victor Stafusa
@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.
– Victor
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?
– Victor Stafusa
@Victorstafusa edited the question.
– Victor
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?
– Victor Stafusa
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.
– Victor
Thanks for the help to all!!!
– Victor