nemequ / portable-snippets

Collection of miscellaneous portable C snippets.
Other
676 stars 66 forks source link

random module needs convenience functions #11

Open nemequ opened 7 years ago

nemequ commented 7 years ago

µnit has some, like munit_rand_int_range. @jibsen, IIRC you did some work on those functions, would you mind if I relicensed them to public domain (CC0) and merged them into psnip?

It would probably also be a good idea to provide psnip_random_int, psnip_random_double… Ideally I'd like to just have a macro like

#define psnip_random_value(source, T) \
  ({ T psinp_random__tmp; \
     psnip_random_bytes(src, sizeof(T), &tmp); \
     psnip_random__tmp;  })

But that's a GCC extension; IIRC clang and icc support it, but I don't think there is a way to implement it with MSVC. I think the best we'll be able to do is something like

#define PSNIP_RANDOM_DEFINE_VALUE_FUNC(T, func_name) \
  __attribute__((__unused__)) \
  static inline T func_name (enum PSnipRandomSource source) { \
    T tmpval_; \
    psnip_random_bytes(source, sizeof(T), &tmpval_); \
    return tmpval_; \
  }

PSNIP_RANDOM_DEFINE_VALUE_FUNC(char, psnip_random_char)
PSNIP_RANDOM_DEFINE_VALUE_FUNC(short, psnip_random_short)
PSNIP_RANDOM_DEFINE_VALUE_FUNC(int, psnip_random_int)
...

With appropriate macros for the unused attribute and the inline keyword, of course.

jibsen commented 7 years ago

µnit has some, like munit_rand_int_range. @jibsen, IIRC you did some work on those functions, would you mind if I relicensed them to public domain (CC0) and merged them into psnip?

Please feel free to do that (but we should probably resolve nemequ/munit#30 first).