diff options
author | Hauke Mehrtens <hauke@hauke-m.de> | 2014-06-08 17:45:44 +0200 |
---|---|---|
committer | Hauke Mehrtens <hauke@hauke-m.de> | 2014-06-16 20:23:37 +0200 |
commit | 7a67db7fabf669af825fa7225295b9d204af2688 (patch) | |
tree | c69d7ce77027db080c9a5bf9cbb6ae74f6833ffb | |
parent | 980f6250683d4c398f3ee753356eb956f29c7832 (diff) |
backports: add prandom_u32_max()
This was added in:
commit f337db64af059c9a94278a8b0ab97d87259ff62f
Author: Daniel Borkmann <dborkman@redhat.com>
Date: Wed Jan 22 02:29:39 2014 +0100
random32: add prandom_u32_max and convert open coded users
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
-rw-r--r-- | backport/backport-include/linux/random.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/backport/backport-include/linux/random.h b/backport/backport-include/linux/random.h index e9151060..dd5f377f 100644 --- a/backport/backport-include/linux/random.h +++ b/backport/backport-include/linux/random.h @@ -13,4 +13,24 @@ void prandom_bytes(void *buf, int bytes); #endif +#if LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0) +/** + * prandom_u32_max - returns a pseudo-random number in interval [0, ep_ro) + * @ep_ro: right open interval endpoint + * + * Returns a pseudo-random number that is in interval [0, ep_ro). Note + * that the result depends on PRNG being well distributed in [0, ~0U] + * u32 space. Here we use maximally equidistributed combined Tausworthe + * generator, that is, prandom_u32(). This is useful when requesting a + * random index of an array containing ep_ro elements, for example. + * + * Returns: pseudo-random number in interval [0, ep_ro) + */ +#define prandom_u32_max LINUX_BACKPORT(prandom_u32_max) +static inline u32 prandom_u32_max(u32 ep_ro) +{ + return (u32)(((u64) prandom_u32() * ep_ro) >> 32); +} +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(3,14,0) */ + #endif /* __BACKPORT_RANDOM_H */ |