Commit 1828f66f authored by James Willis's avatar James Willis
Browse files

Added function to shuffle an array of particles randomly. Moved random_uniform...

Added function to shuffle an array of particles randomly. Moved random_uniform from testPair/test27cells to tools.c.
parent ef91f6c8
......@@ -471,3 +471,33 @@ void engine_single_force(double *dim, long long int pid,
p.a_hydro[1], p.a_hydro[2]);
fflush(stdout);
}
/**
* Returns a random number (uniformly distributed) in [a,b[
*/
double random_uniform(double a, double b) {
return (rand() / (double)RAND_MAX) * (b - a) + a;
}
/**
* @brief Randomly shuffle an array of particles.
*/
void shuffle_particles(struct part *parts, const int count) {
if(count > 1) {
for(int i=0; i<count - 1; i++) {
int j = i + random_uniform(0.,(double)(count - 1 - i));
struct part particle = parts[j];
parts[j] = parts[i];
parts[i] = particle;
}
}
else error("Array not big enough to shuffle!");
}
......@@ -38,4 +38,7 @@ void self_all_density(struct runner *r, struct cell *ci);
void pairs_n2(double *dim, struct part *__restrict__ parts, int N,
int periodic);
double random_uniform(double a, double b);
void shuffle_particles(struct part *parts, const int count);
#endif /* SWIFT_TOOL_H */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment