forked from yanliu/pgap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randseq.c
68 lines (66 loc) · 2.08 KB
/
randseq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/******************************************************************************
* randseq.h: generating unique random sequence *
* Author: Yan Y. Liu <[email protected]> *
* Date: 2014/08/17 *
* Copyright and license of this source file are specified in LICENSE.TXT *
* under the root directory of this software package. *
******************************************************************************/
#ifndef RANDSEQ_C
#define RANDSEQ_C
/* generate a random sequence of size n, the problem size */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "myrng.h"
#include "randseq.h"
// shuffle the random sequence memory using Fisher Yates algorithm
// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
int randseq_shuffle(int *randseq, int size)
{
int i, j, tmp;
for (i=0; i<size-1; i++) {
j = MYRANDI(size-i);
tmp = randseq[i];
randseq[i] = randseq[j];
randseq[j] = tmp;
}
return 1;
}
// init the random sequence memory
int randseq_init(int **randseq_holder, int size) {
if (size <= 0) return 0;
int i;
int *randseq;
if (*randseq_holder == NULL) {
randseq = (int *)malloc(sizeof(int) * size);
if (randseq == NULL) {
fprintf(stderr, "randseq_init(): ERROR: out of memory in getting %d integers.\n", size);
exit(1);
}
*randseq_holder = randseq;
for (i=0; i<size; i++) randseq[i] = i;
randseq_shuffle(randseq, size);
}
return 1;
}
// free memory. NOTE: randseq will NOT be set to NULL after this call. Set it by yourself!!!
int randseq_finalize(int *randseq, int size)
{
if (randseq != NULL) free(randseq);
randseq = NULL;
return 1;
}
int randseq_verify(int *randseq, int size)
{
int i; char ihash[size];
memset(ihash, 0, sizeof(char) * size);
for (i=0; i<size; i++) {
if (randseq[i]<0 || randseq[i]>=size || ihash[randseq[i]] > 0) {
fprintf(stderr, "randseq[%d/%d] = %d. either dup or out of bound\n", i, size, randseq[i]);
return 0;
}
else ihash[randseq[i]] ++;
}
return 1;
}
#endif