-

2011年8月4日星期四

C Programming help (please see description)?

-Uuhhh,, im having a problem. my goal is to generate a number that is non-repeating or doesnt repeat...Ive used srand(time(null)) and rand(); SOMETIMES it generates like this 1 2 4 2 .(4 choices only) As you can see, it generates the number 2 twice. How can I avoid that?Make an array with the numbers that you want to use and scramble that array.



use rand() to pick two positions in the array and swap those two values.

Repeat 10 times more than the number of elements in your array.

It will be well shuffled that way and have no repeats.



something like this (untested)



int j,k,t,n;

int arr[12];

for(j=0;j<12;j++)

arr[j]=j;

for (k=0;k<120;k++){

j=rand()%12;

n=rand()%12;

t=arr[j];

arr[j]=arr[n]

arr[n]=t;

}
may be.........

u can store the number in a array and when the new number is generated compare with the array list.

if no match found store it in array and if found then discard the number



Hope this helps!!!

没有评论:

发表评论