… | |
… | |
63 | |
63 | |
64 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
64 | return state [0] ^ state [1] ^ state [2] ^ state [3]; |
65 | } |
65 | } |
66 | |
66 | |
67 | uint32_t |
67 | uint32_t |
68 | tausworthe_random_generator::get_range (uint32_t r_max) |
68 | tausworthe_random_generator::get_range (uint32_t num) |
69 | { |
69 | { |
70 | return (next () * (uint64_t)r_max) >> 32U; |
70 | return (next () * (uint64_t)num) >> 32U; |
71 | } |
71 | } |
72 | |
72 | |
73 | // return a number within (min .. max) |
73 | // return a number within (min .. max) |
74 | int |
74 | int |
75 | tausworthe_random_generator::get_range (int r_min, int r_max) |
75 | tausworthe_random_generator::get_range (int r_min, int r_max) |
76 | { |
76 | { |
77 | return r_min + get_range (max (r_max - r_min + 1, 1)); |
77 | return r_min + get_range (max (r_max - r_min + 1, 0)); |
78 | } |
78 | } |
79 | |
79 | |
80 | /* |
80 | /* |
81 | * The random functions here take luck into account when rolling random |
81 | * The random functions here take luck into account when rolling random |
82 | * dice or numbers. This function has less of an impact the larger the |
82 | * dice or numbers. This function has less of an impact the larger the |
… | |
… | |
607 | memcpy (dst + maxlen - 4, "...", 4); |
607 | memcpy (dst + maxlen - 4, "...", 4); |
608 | } |
608 | } |
609 | } |
609 | } |
610 | else |
610 | else |
611 | memcpy (dst, src, len + 1); |
611 | memcpy (dst, src, len + 1); |
|
|
612 | } |
|
|
613 | |
|
|
614 | const std::string |
|
|
615 | format (const char *format, ...) |
|
|
616 | { |
|
|
617 | int len; |
|
|
618 | |
|
|
619 | { |
|
|
620 | char buf[128]; |
|
|
621 | |
|
|
622 | va_list ap; |
|
|
623 | va_start (ap, format); |
|
|
624 | len = vsnprintf (buf, sizeof (buf), format, ap); |
|
|
625 | va_end (ap); |
|
|
626 | |
|
|
627 | assert (len >= 0); // shield againstz broken vsnprintf's |
|
|
628 | |
|
|
629 | // was our static buffer short enough? |
|
|
630 | if (len < sizeof (buf)) |
|
|
631 | return std::string (buf, len); |
|
|
632 | } |
|
|
633 | |
|
|
634 | { |
|
|
635 | // longer, try harder |
|
|
636 | char *buf = salloc<char> (len + 1); |
|
|
637 | |
|
|
638 | va_list ap; |
|
|
639 | va_start (ap, format); |
|
|
640 | vsnprintf (buf, len + 1, format, ap); |
|
|
641 | va_end (ap); |
|
|
642 | |
|
|
643 | const std::string s (buf, len); |
|
|
644 | sfree<char> (buf, len + 1); |
|
|
645 | |
|
|
646 | return buf; |
|
|
647 | } |
612 | } |
648 | } |
613 | |
649 | |
614 | tstamp now () |
650 | tstamp now () |
615 | { |
651 | { |
616 | struct timeval tv; |
652 | struct timeval tv; |