-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumtest.c~
73 lines (65 loc) · 1.75 KB
/
numtest.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <assert.h>
#define MAXSTRLEN 256
#define MIN(x, y) ((x)<(y) ? (x):(y))
int bad(char* key, int size){
/* Assume strand() has been called */
static int a;
static int flag = 1;
if (flag == 1){
/* a only changes once*/
/* a is random within [0,size) */
a = rand()%size;
flag ++;
}
return a*key[0]%size;
}
unsigned int universal_hash(unsigned char *key, unsigned int size) {
int len, i, sum;
if (strlen((char*)key) > MAXSTRLEN){
len = MAXSTRLEN;
}else{
len = strlen((char*)key);
}
sum = 0;
for (i=0; i<len; i++)
{
/* Assume strand() has been called */
sum = sum + (rand()%size)*key[i];
}
return sum%size;
}
int main()
{
static int seed = 0;
srand(seed);
int size = 23;
char* key1 = "aa";
char* key2 = "bb";
unsigned char* key3 = (unsigned char*)"aa";
int a = 255;
unsigned char maxplusone = 202;
unsigned char one = 1;
unsigned char key[2];
key[0] = one;
key[1] = one;
printf("\nbad hash val= %d\n", bad(key1,size));
printf("bad hash val= %d\n", bad(key1,size));
printf("bad hash val= %d\n", bad(key2,size));
printf("key length= %d\n",sizeof(key3)/sizeof(unsigned char));
printf("unsigned char len= %d\n",sizeof(unsigned char));
unsigned int b = one *1;
printf("unsigned char * int = %d\n",b);
printf("key length= %d\n",strlen((char*)key3));
printf("universal hash= %d\n",universal_hash(key,size));
printf("min(1,2)=%d",MIN(1,2));
/*printf("bad hash(%s) = %d\n",key2,
universal_hash((unsigned char*)key2,size));*/
/*printf("generated: %c", (unsigned char)(a+1));*/
maxplusone++;
printf("max+1=%c", maxplusone);
return 0;
}