[riot-notifications] [RIOT-OS/RIOT] sys/random: fix distribution of random_uint32_range() (#9410)

Sebastian Meiling notifications at github.com
Tue Jun 26 11:02:33 CEST 2018


smlng commented on this pull request.



> @@ -54,3 +54,36 @@ void random_bytes(uint8_t *target, size_t n)
         *target++ = *random_pos++;
     }
 }
+
+uint32_t random_uint32_range(uint32_t a, uint32_t b)
+{
+    uint32_t range = b - a;
+    uint32_t highest_pow2 = (1 << bitarithm_msb(range));
+    uint32_t random = random_uint32();
+
+    /* check if range is a power of two */
+    if (range && !(range & (range - 1))) {

yeah I think nowadays its hard to outsmart the compiler 😉 So best we can do is go for well documented and structured code.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/9410#discussion_r198066348
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20180626/55060071/attachment-0001.html>


More information about the notifications mailing list