|
Revision 1084, 3.1 kB
(checked in by alpt, 1 year ago)
|
bla bla
|
- Property svn:eol-style set to
native
- Property svn:keywords set to
Author Date Id Revision
|
| Line | |
|---|
| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
#include <stdlib.h> |
|---|
| 21 |
#include <stdio.h> |
|---|
| 22 |
#include <time.h> |
|---|
| 23 |
#include <sys/types.h> |
|---|
| 24 |
#include <unistd.h> |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
inline int rand_range(int _min, int _max) |
|---|
| 28 |
{ |
|---|
| 29 |
return (rand()%(_max - _min + 1)) + _min; |
|---|
| 30 |
} |
|---|
| 31 |
|
|---|
| 32 |
void xsrand(void) |
|---|
| 33 |
{ |
|---|
| 34 |
FILE *fd; |
|---|
| 35 |
int seed; |
|---|
| 36 |
|
|---|
| 37 |
if((fd=fopen("/dev/urandom", "r"))) { |
|---|
| 38 |
fread(&seed, 4,1,fd); |
|---|
| 39 |
fclose(fd); |
|---|
| 40 |
} else |
|---|
| 41 |
seed=getpid() ^ time(0) ^ clock(); |
|---|
| 42 |
|
|---|
| 43 |
srand(seed); |
|---|
| 44 |
} |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
int surandom2(void) |
|---|
| 48 |
{ |
|---|
| 49 |
srand(getpid() ^ clock()); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
int surandom(void) |
|---|
| 53 |
{ |
|---|
| 54 |
FILE *fd; |
|---|
| 55 |
int seed; |
|---|
| 56 |
|
|---|
| 57 |
fd=fopen("/dev/urandom", "r"); |
|---|
| 58 |
fread(&seed, 4,1,fd); |
|---|
| 59 |
fclose(fd); |
|---|
| 60 |
srand(seed); |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
void maketrash(int len, char *garbage) |
|---|
| 65 |
{ |
|---|
| 66 |
int i,e,g; |
|---|
| 67 |
|
|---|
| 68 |
memset(garbage,0,len); |
|---|
| 69 |
for(i=0;i<len;) { |
|---|
| 70 |
e=rand(); |
|---|
| 71 |
g=len-i > sizeof(int) ? sizeof(int) : len-i; |
|---|
| 72 |
memcpy(&garbage[i], &e, g); |
|---|
| 73 |
i+=g; |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
int wrand(u_int nmemb, u_int *w) |
|---|
| 83 |
{ |
|---|
| 84 |
int i, tot_w, r; |
|---|
| 85 |
|
|---|
| 86 |
for(i=0, tot_w=0; i<nmemb; i++) |
|---|
| 87 |
tot_w+=w[i]; |
|---|
| 88 |
|
|---|
| 89 |
if(!tot_w) |
|---|
| 90 |
return rand_range(0, nmemb-1); |
|---|
| 91 |
|
|---|
| 92 |
r=rand_range(1, tot_w); |
|---|
| 93 |
|
|---|
| 94 |
for(i=0, tot_w=0; i<nmemb; i++) { |
|---|
| 95 |
if(r > tot_w && (r <= tot_w+w[i])) |
|---|
| 96 |
return i; |
|---|
| 97 |
tot_w+=w[i]; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
return -1; |
|---|
| 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 |
int get_rand_interval(void) |
|---|
| 128 |
{ |
|---|
| 129 |
struct timeval t1, t2; |
|---|
| 130 |
int i; |
|---|
| 131 |
|
|---|
| 132 |
gettimeofday(&t1, 0); |
|---|
| 133 |
for(i=0; i<512; i++) { |
|---|
| 134 |
int e=0; |
|---|
| 135 |
char buf[512]; |
|---|
| 136 |
|
|---|
| 137 |
for(e=0; e<512; e++) |
|---|
| 138 |
buf[e]=((e*i*e*i)/( !i ? 7 : i+1))+e-i; |
|---|
| 139 |
} |
|---|
| 140 |
gettimeofday(&t2, 0); |
|---|
| 141 |
|
|---|
| 142 |
return abs((t2.tv_usec-t1.tv_usec)+(t2.tv_sec-t1.tv_sec)); |
|---|
| 143 |
} |
|---|