void generateRandomNumbers(int x[], int n)
{
int i, j, t;
for(i=0; i < n; i++)
x[i] = i;
for(i=0; i < n; i++) {
j = random(n-i);
t = x[i];
x[i] = x[j];
x[j] = t;
}
}
The complexity of this solution is O(n).
void generateRandomNumbers(int x[], int n)
{
int i, j, t;
for(i=0; i < n; i++)
x[i] = i;
for(i=0; i < n; i++) {
j = random(n-i);
t = x[i];
x[i] = x[j];
x[j] = t;
}
}
i think this also works
ReplyDeletevoid main()
{
int n,i;
printf("enter n value");
scanf("%d",&n);
randomize();
for(i=0;i<=n;i++)
printf("%d",random(i));
}
This does not generate unique random numbers between 1 to n.
ReplyDeleteThis always gives first element as 0. In the first x elements, you will never see any number more than x. This is not random.
ya agree wid u ,
ReplyDeleteit wud be random(n)
If it is random(n), then it is not guaranteed to be unique. If you call random(n) twice, you may get the same no both times. But, we need unique random numbers.
ReplyDelete