How can I asign a random values to an array without repeating the value? Please help.
Dear Cprogramming members,
Hello and good day to everyone. I just want to ask how would I assign a value from 0 to 8 only in an array without repeating its value? I have currently type this code in my program:
Code:
int x, num[9];
.
.
.
for (x=0; x<=8; x++)
{
num[x] = rand() % 9;
}
But as I watched the value of variables, it seem that there are numbers between 0 to 8 that has been repeated twice, thrice, etc. Is there a way that I could assigned a random but nonrepeating values in an array? Example, if in the first run, the value 4 was assigned in num[0], then value 4 will not be then again assigned to any of the remaining empty array, only 0,1,2,3,5,6,7,8 is only allowed. And I noticed that everytime I run the program, num[0] is always 4, is there a way I could change it?
Thank you and more power.
Respectfully yours,
MarkSquall