-
Notifications
You must be signed in to change notification settings - Fork 0
/
ca-prng-3state.c
133 lines (110 loc) · 2.9 KB
/
ca-prng-3state.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <string.h>
#define N 51
#define d 3
#define u 32
#define w 20
void main()
{
int i;
unsigned int random_number = 0;
int rule[] = {0, 1, 2, 1, 2, 0, 1, 2, 0, 0, 2, 1, 1, 2, 0, 1, 2, 0, 0, 2, 1, 1, 2, 0, 0, 2, 1};
FILE *fp;
int castate[N], state[N], cnt = 0, deci, bin[u], window[w];
unsigned int dc = 0, index = 1;
int j;
for (i = 0; i < 20; i++)
{
window[i] = 0;
}
fp = fopen("abc.bin", "w"); // Open The File
if (fp == NULL)
{
printf("file not found\n");
exit(0);
}
for (i = 0; i < N; i++)
state[i] = 0;
printf("\nEnter %d random ternary value for the window : \n", w);
for (i = 0; i < w; i++)
{
scanf("%d", &state[i]);
}
for (i = w; i < N - 1; i++) //initialize other values to 000...001
state[i] = 0;
state[N - 1] = 1;
/*// Next state generation
NS[0] = Rule[d*d*PS[n-1]+d*PS[0]+PS[1]]; // next state for left most cell
for(i=1;i<n-1;i++)
{
NS[i] = Rule[d*d*PS[i-1]+d*PS[i]+PS[i+1]];
}
NS[n-1] = Rule[d*d*PS[n-2]+d*PS[n-1]+PS[0]]; // next state for right most cell
for(i=0;i<n;i++)
PS[i] = NS[i];
printf("\n Random Number : ");
for(i=0;i<window;i++){
random_number += PS[i]*pow(d,window-1-i); //converts to decimal
printf("%d",PS[i]);
}
printf("\t = %u (in decimal)\n", random_number);
//return(random_number);*/
for (i = 0; i < N; i++)
printf("%d", state[i]); //print initial state
printf("\n\n");
char ch = 0, s = 0;
while (cnt < 3200000) // calculating Next state
{
deci = state[N - 1] * 9 + state[0] * 3 + state[1];
castate[0] = rule[deci];
for (i = 1; i < N - 1; i++)
{
deci = state[i - 1] * 9 + state[i] * 3 + state[i + 1];
castate[i] = rule[deci];
}
deci = state[N - 2] * 9 + state[N - 1] * 3 + state[0];
castate[N - 1] = rule[deci];
for (i = 0; i < N; i++)
{
state[i] = castate[i];
}
for (i = 0; i < w; i++)
window[i] = state[i]; //populate window
for (i = 0; i < u; i++)
bin[i] = 0;
for (i = 0; i < w; i++) //Decimal Value
{
dc += window[w - 1 - i] * index;
index *= 3;
}
for (i = 0; i < u; i++) //Binary Value
{
bin[i] = dc & 1;
dc = dc >> 1;
}
/***** (Binary)*******/
for (i = 0; i < u; i++)
{
ch = (ch << 1) | bin[i];
if (s == 7)
{
//printf("%c",ch);
fprintf(fp, "%c", ch);
s = 0;
ch = 0;
}
else
{
s++;
}
} // End of Binary
dc = 0;
index = 1;
cnt++;
}
cnt = 0;
fclose(fp);
}