Hey all,

Right now I’m struggling with coming with an algorithm or any idea as to how randomly shuffle an array of known size.

```
__global__ void shuffle( int *a, int n ) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= n) {
return;
}
int random = n * BLAH; // I'm using a Mersenne Twister to generate a random number between 0 and 1
int old = atomicExch( &a[random], a[idx] );
a[idx] = old;
}
int main() {
const int n = 1000;
int numThreadsPerBlock = 512;
int numBlocks = (int) ceil(n / numThreadsPerBlock)+1;
dim3 dimGrid(numBlocks);
dim3 dimBlock(numThreadsPerBlock);
int a[n];
for (int i = 0; i < n; i++) {
a[i] = i;
}
int *a_d;
cudaMalloc( (void **) &a_d, n * sizeof(int));
cudaMemcpy( a_d, a, n * sizeof(int), cudaMemcpyHostToDevice);
shuffle <<< dimGrid, dimBlock, n*sizeof(int) >>> ( a_d, b_d, n );
cudaMemcpy( a, a_d, n* sizeof(int), cudaMemcpyDeviceToHost);
for (int i = 0; i < n; i++) {
printf("%d\n", a[i]);
}
}
```

This, of course, does not work since in the end there will be duplicate values of a, since the same values for the random variable will inevitably be generated across at least 2 threads, creating a semi - race condition ( I think? ).

Are there any implementations anyone can think of? Or am I doing something wrong?

Thanks!