memcpy

stronger random generator

Dec 19th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/sysinfo.h>
  4. #include <sys/time.h>
  5. #include <stdint.h>
  6.  
  7. #define RP_POOL_MAX 10
  8.  
  9. #define rp_ins_pool(v, p)                   \
  10.                         while ( *p != 0)    \
  11.                             p++;            \
  12.                         *p = v;    
  13.  
  14. struct rand_pool {
  15.  
  16.     int64_t rp_mem_sum;
  17.     int64_t rp_ram_sum;
  18.     int32_t rp_sys_time;
  19.    
  20.     int64_t rp_top_limit;
  21.     int64_t rp_btm_limit;
  22.    
  23.     int64_t *rp_pool;
  24. };
  25.  
  26. static int64_t rp_parse_time() {
  27.  
  28.     struct timeval *tv = malloc(sizeof *tv);
  29.     gettimeofday(tv, NULL);
  30.    
  31.     int32_t l_s = (int32_t)(tv->tv_sec / 1000);
  32.    
  33.     int32_t l_u = (int32_t)(tv->tv_usec * 1000);
  34.    
  35.     int64_t l_r = (l_s + l_u);
  36.  
  37.     free(tv);
  38.     return (l_r ? l_r : l_u * l_s);
  39. }
  40.  
  41. static int64_t rp_parse_mem() {
  42.    
  43.     struct sysinfo *sf = malloc(sizeof *sf);
  44.     sysinfo(sf);
  45.    
  46.     int32_t l_s = sf->loads[0] + sf->loads[1] + sf->loads[2];
  47.    
  48.     int16_t l_d = (l_s | (sf->procs % 2 ? 0x6 : 0x2));
  49.    
  50.     int64_t l_q = (int64_t)(sf->uptime * sf->freeram) / l_d;
  51.  
  52.     int64_t l_r = (sf->totalswap << (l_q / 1000));
  53.    
  54.     free(sf);
  55.     return (l_r ? l_r : l_s * l_d);
  56. }
  57.  
  58. static int64_t rp_parse_ram() {
  59.  
  60.     struct sysinfo *sf = malloc(sizeof *sf);
  61.     sysinfo(sf);
  62.    
  63.     int64_t l_r = (sf->totalram * sf->bufferram) >> 32;
  64.  
  65.     int64_t l_c = (int64_t)(int32_t)(l_r * sf->freeram);
  66.  
  67.     free(sf);
  68.     return (l_c ? l_c : l_r * l_r);
  69. }
  70.  
  71. static struct rand_pool *rp_init_pool() {
  72.  
  73.     struct rand_pool *rp;
  74.     rp = calloc(1, sizeof *rp);
  75.  
  76.     rp->rp_pool = calloc(1, RP_POOL_MAX + 1);
  77.  
  78.     rp->rp_sys_time = rp_parse_time();
  79.  
  80.     rp->rp_ram_sum = rp_parse_ram();
  81.  
  82.     rp->rp_mem_sum = rp_parse_mem();
  83.  
  84.     rp_ins_pool(rp->rp_sys_time, rp->rp_pool);
  85.     rp_ins_pool(rp->rp_ram_sum, rp->rp_pool);
  86.     rp_ins_pool(rp->rp_mem_sum, rp->rp_pool);
  87.    
  88.     return rp;
  89. }
  90.  
  91. static int64_t rp_synth_pool(struct rand_pool *rp) {
  92.    
  93.     int64_t ret;
  94.    
  95.     int64_t l_m = (rp->rp_mem_sum < rp->rp_ram_sum ?
  96.                     rp->rp_ram_sum : rp->rp_mem_sum);
  97.                    
  98.     int64_t l_t = (l_m > 1073741824 ?
  99.                     l_m / 1073741824 :
  100.                     (int64_t)((float)l_m * 1.25));
  101.    
  102.     int64_t l_f = (l_t * (rp->rp_sys_time / 1000));
  103.    
  104.     int64_t l_mod_top = rp->rp_top_limit - rp->rp_btm_limit;   
  105.    
  106.     int64_t l_ret = ( (l_f + rp->rp_btm_limit) % l_mod_top);   
  107.  
  108.     if ((0 > rp->rp_btm_limit) && (l_ret % 2 || l_ret % 3))
  109.         return 0-l_ret;
  110.     else
  111.         return l_ret;
  112. }
  113.  
  114. static int64_t rp_rand(int64_t top, int64_t btm) {
  115.  
  116.     struct rand_pool *rp;
  117.    
  118.     rp = rp_init_pool();
  119.    
  120.     rp->rp_top_limit = top;
  121.     rp->rp_btm_limit = btm;
  122.    
  123.     int64_t rp_final;
  124.     rp_final = rp_synth_pool(rp);
  125.            
  126.     free(rp);
  127.    
  128.     return rp_final;
  129.  
  130. }
  131.  
  132. int main() {
  133.  
  134.     int64_t prnt;
  135.     int64_t top = 10000000;
  136.     int64_t btm = -10000000;
  137.    
  138.     for (;;) {
  139.         prnt = rp_rand(top, btm);
  140.         printf("Random between %ld and %ld: %ld\n", btm, top, prnt);
  141.         sleep(1);
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment