root/misc/rnd.c

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 /* Random collection of random functions
2  * AlpT <alpt@freaknet.org>
3  *
4  * This source code is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This source code is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * Please refer to the GNU Public License for more details.
13  *
14  * You should have received a copy of the GNU Public License along with
15  * this source code; if not, write to:
16  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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  * wrand
79  *
80  * Weighted rand
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  * get_rand_interval
105  *
106  * It returns a real random integer.
107  *
108  * The idea is simple: it counts how many microseconds passes from the start
109  * to the end of a simple loop that writes for 262144 times on a buffer.
110  * The interval is then returned.
111  *
112  * The interval is influenced chaotically by all the running programs on the
113  * OS and by the kernel itself. The computer is then influenced by the user
114  * interference, the attached devices, the generated interrupts, etc...
115  * For this reason, a same function, will always require different time
116  * intervals to solve its computation.
117  *
118  * You should not use get_rand_interval() to get a random number, because the
119  * returned intervals are not (luckily) uniformly distributed, moreover it
120  * requires a lot of computation.
121  * The correct use of this function is to set the random seed using srand(),
122  * seed48(), _only if_ /dev/urandom is not present in the OS.
123  *
124  * Thanks to /dev/random for the inspiration (it measures the interval between
125  * interrupts, etc...)
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 }
Note: See TracBrowser for help on using the browser.