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

Random shuffle and related functions. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpcextensions.h"
#include "tpcrand.h"

Go to the source code of this file.

Functions

void randomShuffle (int *array, unsigned int n, MERTWI *mt)
 Random shuffle.
void randomShuffleUnsigned (unsigned int *array, unsigned int n, MERTWI *mt)
 Random shuffle.
void randomPermutation (int *array, unsigned int n, int a, MERTWI *mt)
 Random permutation for an array of signed integers.
void randomPermutationUnsigned (unsigned int *array, unsigned int n, unsigned int a, MERTWI *mt)
 Random permutation for an array of unsigned integers.

Detailed Description

Random shuffle and related functions.

Definition in file shuffle.c.

Function Documentation

◆ randomPermutation()

void randomPermutation ( int * array,
unsigned int n,
int a,
MERTWI * mt )

Random permutation for an array of signed integers.

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

Precondition
Prepare the Mersenne Twister, for example using code MERTWI mt; mertwiInit(&mt); mertwiInitWithSeed64(&mt, mertwiSeed64());
See also
randomPermutationUnsigned, randomShuffle
Parameters
arrayAllocated array to be filled.
nLength of array.
aSmallest integer value.
mtPointer to data structure for Mersenne Twister MT19937 pseudo-random number generator.

Definition at line 80 of file shuffle.c.

89 {
90 if(n<1 || array==NULL) return;
91 int i;
92 for(i=0; i<(int)n; i++) array[i]=i+a;
93 randomShuffle(array, n, mt);
94}
void randomShuffle(int *array, unsigned int n, MERTWI *mt)
Random shuffle.
Definition shuffle.c:27

◆ randomPermutationUnsigned()

void randomPermutationUnsigned ( unsigned int * array,
unsigned int n,
unsigned int a,
MERTWI * mt )

Random permutation for an array of unsigned integers.

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

Precondition
Prepare the Mersenne Twister, for example using code MERTWI mt; mertwiInit(&mt); mertwiInitWithSeed64(&mt, mertwiSeed64());
See also
randomPermutation, randomShuffleUnsigned
Parameters
arrayAllocated array to be filled.
nLength of array.
aSmallest integer value.
mtPointer to data structure for Mersenne Twister MT19937 pseudo-random number generator.

Definition at line 106 of file shuffle.c.

115 {
116 if(n<1 || array==NULL) return;
117 unsigned int i;
118 for(i=0; i<n; i++) array[i]=i+a;
119 randomShuffleUnsigned(array, n, mt);
120}
void randomShuffleUnsigned(unsigned int *array, unsigned int n, MERTWI *mt)
Random shuffle.
Definition shuffle.c:54

◆ randomShuffle()

void randomShuffle ( int * array,
unsigned int n,
MERTWI * mt )

Random shuffle.

Arrange the n elements of integer array in random order. Only effective if N is much smaller than UINT64_MAX.

Precondition
Prepare the Mersenne Twister, for example using code MERTWI mt; mertwiInit(&mt); mertwiInitWithSeed64(&mt, mertwiSeed64());
See also
randomShuffleUnsigned
Parameters
arrayInteger array to be shuffled.
nArray length.
mtPointer to data structure for Mersenne Twister MT19937 pseudo-random number generator.

Definition at line 27 of file shuffle.c.

34 {
35 if(n<=1 || array==NULL) return;
36 unsigned int i, j;
37 int tmp;
38 for(i=0; i<n-1; i++) {
39 j=i+mertwiRandomInt64(mt)/(UINT64_MAX/(n-i)+1);
40 tmp=array[j]; array[j]=array[i]; array[i]=tmp;
41 }
42}
uint64_t mertwiRandomInt64(MERTWI *mt)
Generate a random number on [0, 2^64-1]-interval using Mersenne Twister MT19937.
Definition mertwi.c:153

Referenced by randomPermutation().

◆ randomShuffleUnsigned()

void randomShuffleUnsigned ( unsigned int * array,
unsigned int n,
MERTWI * mt )

Random shuffle.

Arrange the n elements of unsigned integer array in random order. Only effective if N is much smaller than UINT64_MAX.

Precondition
Prepare the Mersenne Twister, for example using code MERTWI mt; mertwiInit(&mt); mertwiInitWithSeed64(&mt, mertwiSeed64());
See also
randomShuffle, randomPermutation
Parameters
arrayInteger array to be shuffled.
nArray length.
mtPointer to data structure for Mersenne Twister MT19937 pseudo-random number generator.

Definition at line 54 of file shuffle.c.

61 {
62 if(n<=1 || array==NULL) return;
63 unsigned int i, j, tmp;
64 for(i=0; i<n-1; i++) {
65 j=i+mertwiRandomInt64(mt)/(UINT64_MAX/(n-i)+1);
66 tmp=array[j]; array[j]=array[i]; array[i]=tmp;
67 }
68}

Referenced by randomPermutationUnsigned().