From: tyson.w.smith@gmail.com
To: davej@codemonkey.org.uk
Cc: trinity@vger.kernel.org, Tyson Smith <tysmith@motorola.com>
Subject: [PATCH 02/12] Created RAND_32 and RAND_64 macros.
Date: Fri, 6 Mar 2015 14:29:29 -0800 [thread overview]
Message-ID: <1425680969-28361-1-git-send-email-tyson.w.smith@gmail.com> (raw)
From: Tyson Smith <tysmith@motorola.com>
These macros will generate a random number between 0 and 2^32 - 1 or
2^64 - 1 respectively. There is also a compile time check to verify
that a full 2^n bits of randomness will be generated (depends on RAND_MAX).
---
include/random.h | 8 ++++++++
random.c | 4 ++--
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/include/random.h b/include/random.h
index dbff344..f070568 100644
--- a/include/random.h
+++ b/include/random.h
@@ -1,10 +1,18 @@
#pragma once
+#include <stdlib.h>
#include "child.h"
#include "types.h"
#define ONE_IN(x) ((rand() % x) == 0) // limit of RAND_MAX-1
+#if RAND_MAX == 0x7FFFFFFF
+#define RAND_32() ((rand() << 1) | (rand() & 1))
+#define RAND_64() (((0ULL | rand()) << 33) | ((0ULL | rand()) << 2) | (rand() & 0x3))
+#else
+#error "Unexpected RAND_MAX value. Please add support."
+#endif
+
extern unsigned int seed;
unsigned int init_seed(unsigned int seed);
void set_seed(struct childdata *child);
diff --git a/random.c b/random.c
index 6a2e90e..f425ed5 100644
--- a/random.c
+++ b/random.c
@@ -132,7 +132,7 @@ static unsigned int __rand32(void)
break;
case 1: r = randbits(32);
break;
- case 2: r = rand();
+ case 2: r = RAND_32();
break;
case 3: r = rand8x8();
break;
@@ -216,7 +216,7 @@ u64 rand64(void)
break;
case 1: r = randbits(64);
break;
- case 2: r = rand32() | rand32() << 31;
+ case 2: r = RAND_64();
break;
case 3: r = rand8x8();
break;
--
1.9.1
reply other threads:[~2015-03-06 22:29 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1425680969-28361-1-git-send-email-tyson.w.smith@gmail.com \
--to=tyson.w.smith@gmail.com \
--cc=davej@codemonkey.org.uk \
--cc=trinity@vger.kernel.org \
--cc=tysmith@motorola.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).