split up constants.h some
[trinity.git] / random-length.c
blob2fe2538402bf34f55e90e5655cf6663e3a1ba33c
1 #include <stdlib.h>
3 #include "arch.h" // page_size
4 #include "sanitise.h"
5 #include "random.h"
7 unsigned long get_len(void)
9 int i = 0;
11 i = rand32();
13 /* short circuit if 0 */
14 if (i == 0)
15 return 0;
17 switch (rand() % 6) {
19 case 0: i &= 0xff;
20 break;
21 case 1: i &= page_size;
22 break;
23 case 2: i &= 0xffff;
24 break;
25 case 3: i &= 0xffffff;
26 break;
27 case 4: i &= 0xffffffff;
28 break;
29 case 5:
30 // Pass through
31 break;
34 /* again, short circuit if 0 */
35 if (i == 0)
36 return 0;
38 /* we might get lucky if something is counting ints/longs etc. */
39 if (rand() % 100 < 25) {
40 int _div = 1 << ((rand() % 4) + 1); /* 2,4,8 or 16 */
41 i /= _div;
44 return i;