Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ./main > out.txt && grep -o "$(head -c 10000 ./out.txt | tail -c 16)" ./out.txt | wc -l
- // ./main | dieharder -B -g 200 -a
- #include <stdio.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <limits.h>
- #include <string.h>
- typedef uint8_t cygonUI;
- cygonUI cgRor(cygonUI num, cygonUI shift){
- cygonUI invShift=sizeof(num)*CHAR_BIT-shift;
- return ((num >> shift) | (num << invShift ));
- }
- typedef struct cygonNode{
- cygonUI num;
- struct cygonNode* next;
- }cygonNode;
- typedef struct cygonList{
- size_t bits; //bit width of cygonUI
- size_t size;
- cygonNode* node; //list of nodes
- cygonNode** mem; //Mallocated node list adresses
- }cygonList;
- void nextNode(cygonList* list) {
- list->node=list->node->next;
- }
- void cygonInit(cygonList* list, size_t nodeCount) {
- if (nodeCount < 1) {
- fprintf(stderr, "List can't have 0 nodes! Using size 1\n");
- nodeCount = 1;
- }
- list->bits = sizeof(cygonUI) * CHAR_BIT;
- list->size = nodeCount;
- list->node = (cygonNode*) malloc(sizeof(cygonNode) * nodeCount);
- list->mem = (cygonNode**) malloc(sizeof(cygonNode*) * nodeCount); // allocate node pointers
- if (list->node == NULL || list->mem == NULL) {
- fprintf(stderr, "Memory allocation failed\n");
- return;
- }
- // Link circular list
- for (size_t i = 0; i < nodeCount; ++i) {
- list->mem[i] = &list->node[i]; //Store mallocated address of node
- list->node[i].num = 0;
- list->node[i].next = &list->node[(i + 1) % nodeCount];
- }
- }
- void cygonFree(cygonList* list) {
- free(*list->mem);
- free(list->mem);
- }
- void cygonSeed(cygonList* list, cygonUI seed) {
- cygonNode* start = list->node; //store starting point
- do{
- list->node->num=seed;
- nextNode(list);
- {//placeholder
- seed++;
- }
- }while(list->node != start); //Reaching starting point means we have looped through circular list
- }
- cygonUI cygonRand(cygonList* list) {
- cygonUI a = list->node->num;
- cygonUI b = list->node->next->num;
- {//placeholder
- a++;
- }
- //update and increment circular list
- list->node->num=a;
- list->node=list->node->next;
- return a;
- }
- //print all nodes
- void cygonPrint(cygonList* list) {
- cygonNode* start = list->node;
- do{
- printf("%u ",list->node->num);
- list->node=list->node->next;
- }while(list->node != start);
- printf("\n");
- }
- size_t randCountArg(int argc, char *argv) {
- if (argc > 1) {
- if(!strcmp(argv,"default")){
- return 512;
- }else{
- char isString=1;
- char *x = argv;
- while (*x) {
- if (!isdigit(*x)) {
- isString=0;
- break;
- }
- x++;
- }
- if(isString){
- return strtoul(argv, NULL, 10);
- }
- }
- }else{
- return -1; //generate indefinitely
- }
- }
- int main(int argc, char *argv[]) {
- size_t count = randCountArg(argc, argv[1]);
- cygonList list;
- cygonInit(&list, 4);
- cygonSeed(&list, 0);
- for(size_t i=0;i<count;i++){
- cygonUI num = cygonRand(&list);
- //fwrite(&num, sizeof(cygonUI), 1, stdout); //write raw
- printf("%u ",num);
- }
- printf("\n");
- cygonFree(&list);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement