TPCCLIB
Loading...
Searching...
No Matches
shuffle.c File Reference

Random shuffle and related functions. More...

#include "libtpcmodel.h"

Go to the source code of this file.

Functions

void random_shuffle (int *array, int n)
 
void randperm (int *array, int n, int a)
 

Detailed Description

Random shuffle and related functions.

Author
Vesa Oikonen

Definition in file shuffle.c.

Function Documentation

◆ random_shuffle()

void random_shuffle ( int * array,
int n )

Random shuffle: arrange the n elements of array in random order. Only effective if N is much smaller than RAND_MAX.

Definition at line 13 of file shuffle.c.

14{
15 if(n<=1 || array==NULL) return;
16 int i, j, tmp;
17 for(i=0; i<n-1; i++) {
18 j=i+rand()/(RAND_MAX/(n-i)+1);
19 tmp=array[j]; array[j]=array[i]; array[i]=tmp;
20 }
21}
#define RAND_MAX
Definition gaussdev.c:13

Referenced by randperm().

◆ randperm()

void randperm ( int * array,
int n,
int a )

Random permutation: given (allocated) array of length n is filled with random permutation of numbers in the range [a:n+a-1]; that is, each number once in random order.

Definition at line 29 of file shuffle.c.

30{
31 if(n<1 || array==NULL) return;
32 int i;
33 for(i=0; i<n; i++) array[i]=i+a;
34 random_shuffle(array, n);
35}
void random_shuffle(int *array, int n)
Definition shuffle.c:13