Hi, Initially I wanted to generate a random permutation from 1 to n, and n can be a very big number, say 1M, I tried the following code I wrote, but it has its limit to 25199, the problem is with this line j=d%rand()%(n-t); but I don't know how to fix it. When I wrote a simpler printf("%ld",d%rand()); it has no limit at all.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
long *perm(long n){
long *a=malloc(4*n*sizeof(long));
long k;
time_t b;
b=time(NULL);
long d;
d=(long)b;
for(k=0;k<n;k++)a[k]=k+1; //a[]is from 1 to n;
long t,q;
for(q=0;q<1;q++){ //we put q is to do q times random exchange, greater q will decrease the limit.
for(t=0;t<n;t++){
long j;
j=d%rand()%(n-t); // the problem is with this line
long temp;
temp=a[t];
a[t]=a[t+j];
a[t+j]=temp;
}
}
return a;
}
void printarray(long n,long *a)
{
long k=0;
for(k=0;k<n;k++){
if(k%10==0)
printf("\n");
printf("%d\t",a[k]);}
}
long randperm(long n){
long *a;
a=perm(n);
printarray(n,a);
}
main(){
long n;
scanf("%ld",&n);
randperm(n);}