Skip to contentSkip to conversation

Remove static from rand_single_bit()

PATCH New [email protected] 1 participant 1 message Mar 6, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 4/12 Remove static from rand_single_bit()

From: Tyson Smith <[email protected]>

Added a check to verify input value is between 0 and 32 or 64 bits depending on __WORDSIZE.

---
 include/random.h | 1 +
 random.c         | 8 +++++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
include/random.h +1 -0
index f070568..69ccfa3 100644
@@ -24,4 +24,5 @@ unsigned int rand_bool(void);
 unsigned int rand32(void);
 u64 rand64(void);
 unsigned int rand_range(unsigned int min, unsigned int max);
+unsigned long rand_single_bit(unsigned char size);
 unsigned long set_rand_bitmask(unsigned int num, const unsigned long *values);
random.c +7 -1
index adee25e..063912f 100644
@@ -69,8 +69,14 @@ unsigned int rand_bool(void)
 	return rand() % 2;
 }
 
-static unsigned int rand_single_bit(unsigned char size)
+/*
+ * Pick a random power of two between 2^0 and 2^(__WORDSIZE-1)
+ */
+unsigned long rand_single_bit(unsigned char size)
 {
+	if (size > __WORDSIZE)
+		size = __WORDSIZE;
+
 	return (1UL << (rand() % size));
 }
 
Signature · 1 line
1.9.1