[PATCH] divesite.c: use rand() instead of random()

Lubomir I. Ivanov neolit123 at gmail.com
Sat Feb 14 06:16:03 PST 2015


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

random() is POSIX and seems to be missing in MINGW.
it can return a 64bit (if 'long' is 64bit) but
given ID's are 32bit rand() should suffice.

also random() is technically a better algorithm but for
cryptographically unsafe usage like generating IDs the
stdlib's LCPRNG rand() should siffuce.

also this patch makes it so that a true 32bit random value
is returned. how random it is, is another topic.

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
--
---
 divesite.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/divesite.c b/divesite.c
index 4f23601..f7f6afe 100644
--- a/divesite.c
+++ b/divesite.c
@@ -39,8 +39,12 @@ static uint32_t dive_site_getUniqId()
 {
 	uint32_t id = 0;
 
-	while (id == 0 || get_dive_site_by_uuid(id))
-		id = random() + random();
+	while (id == 0 || get_dive_site_by_uuid(id)) {
+		id = rand() & 0xff;
+		id |= (rand() & 0xff) << 8;
+		id |= (rand() & 0xff) << 16;
+		id |= (rand() & 0xff) << 24;
+	}
 
 	return id;
 }
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list