Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/sysinfo.h>
- #include <sys/time.h>
- #include <stdint.h>
- #define RP_POOL_MAX 10
- #define rp_ins_pool(v, p) \
- while ( *p != 0) \
- p++; \
- *p = v;
- struct rand_pool {
- int64_t rp_mem_sum;
- int64_t rp_ram_sum;
- int32_t rp_sys_time;
- int64_t rp_top_limit;
- int64_t rp_btm_limit;
- int64_t *rp_pool;
- };
- static int64_t rp_parse_time() {
- struct timeval *tv = malloc(sizeof *tv);
- gettimeofday(tv, NULL);
- int32_t l_s = (int32_t)(tv->tv_sec / 1000);
- int32_t l_u = (int32_t)(tv->tv_usec * 1000);
- int64_t l_r = (l_s + l_u);
- free(tv);
- return (l_r ? l_r : l_u * l_s);
- }
- static int64_t rp_parse_mem() {
- struct sysinfo *sf = malloc(sizeof *sf);
- sysinfo(sf);
- int32_t l_s = sf->loads[0] + sf->loads[1] + sf->loads[2];
- int16_t l_d = (l_s | (sf->procs % 2 ? 0x6 : 0x2));
- int64_t l_q = (int64_t)(sf->uptime * sf->freeram) / l_d;
- int64_t l_r = (sf->totalswap << (l_q / 1000));
- free(sf);
- return (l_r ? l_r : l_s * l_d);
- }
- static int64_t rp_parse_ram() {
- struct sysinfo *sf = malloc(sizeof *sf);
- sysinfo(sf);
- int64_t l_r = (sf->totalram * sf->bufferram) >> 32;
- int64_t l_c = (int64_t)(int32_t)(l_r * sf->freeram);
- free(sf);
- return (l_c ? l_c : l_r * l_r);
- }
- static struct rand_pool *rp_init_pool() {
- struct rand_pool *rp;
- rp = calloc(1, sizeof *rp);
- rp->rp_pool = calloc(1, RP_POOL_MAX + 1);
- rp->rp_sys_time = rp_parse_time();
- rp->rp_ram_sum = rp_parse_ram();
- rp->rp_mem_sum = rp_parse_mem();
- rp_ins_pool(rp->rp_sys_time, rp->rp_pool);
- rp_ins_pool(rp->rp_ram_sum, rp->rp_pool);
- rp_ins_pool(rp->rp_mem_sum, rp->rp_pool);
- return rp;
- }
- static int64_t rp_synth_pool(struct rand_pool *rp) {
- int64_t ret;
- int64_t l_m = (rp->rp_mem_sum < rp->rp_ram_sum ?
- rp->rp_ram_sum : rp->rp_mem_sum);
- int64_t l_t = (l_m > 1073741824 ?
- l_m / 1073741824 :
- (int64_t)((float)l_m * 1.25));
- int64_t l_f = (l_t * (rp->rp_sys_time / 1000));
- int64_t l_mod_top = rp->rp_top_limit - rp->rp_btm_limit;
- int64_t l_ret = ( (l_f + rp->rp_btm_limit) % l_mod_top);
- if ((0 > rp->rp_btm_limit) && (l_ret % 2 || l_ret % 3))
- return 0-l_ret;
- else
- return l_ret;
- }
- static int64_t rp_rand(int64_t top, int64_t btm) {
- struct rand_pool *rp;
- rp = rp_init_pool();
- rp->rp_top_limit = top;
- rp->rp_btm_limit = btm;
- int64_t rp_final;
- rp_final = rp_synth_pool(rp);
- free(rp);
- return rp_final;
- }
- int main() {
- int64_t prnt;
- int64_t top = 10000000;
- int64_t btm = -10000000;
- for (;;) {
- prnt = rp_rand(top, btm);
- printf("Random between %ld and %ld: %ld\n", btm, top, prnt);
- sleep(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment