sha0.miller
BAN USERHere is a simple example implementation of getRandomTripplet (I know that's not what the question is asking for):
#include <stdlib.h>
#include <stdio.h>
#define TRIPLET 3
static char secret[] = "helloworld";
static int getRandomTripplet(char * buf);
int main(void) {
char buf[TRIPLET + 1] = "";
int i;
int sz;
/* Fetch the size */
sz = getRandomTripplet(NULL);
/* Test the generator */
for (i = 0; i < 512; ++i) {
getRandomTripplet(buf);
printf("%s\n", buf);
}
return EXIT_SUCCESS;
}
static int getRandomTripplet(char * buf) {
unsigned char positions[sizeof secret - 1] = "";
unsigned int pos;
double d;
int i;
if (buf) {
i = 0;
while (1) {
d = rand();
d /= RAND_MAX;
d *= sizeof secret - 1;
pos = d;
if (positions[pos])
continue;
positions[pos] = 1;
++i;
if (i == 3)
break;
}
i = 0;
for (pos = 0; pos < sizeof positions; ++pos) {
if (positions[pos])
buf[i++] = secret[pos];
}
}
return sizeof secret;
}
Here is my first try:
- sha0.miller July 15, 2014