diff options
author | Nick Mathewson <nickm@torproject.org> | 2010-06-22 21:30:26 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2010-06-22 21:30:26 -0400 |
commit | 006e2e8620fa9001c8df24463c06538cf58ce396 (patch) | |
tree | 23964dd0db4cec3dbcd6e22f0c70104c21e07a3f /src/common | |
parent | e2b117eb806dd517867f99519e2f74a3b2ea0072 (diff) | |
download | tor-006e2e8620fa9001c8df24463c06538cf58ce396.tar.gz tor-006e2e8620fa9001c8df24463c06538cf58ce396.zip |
Add a function to return a double in range [0,1).
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/crypto.c | 15 | ||||
-rw-r--r-- | src/common/crypto.h | 1 |
2 files changed, 16 insertions, 0 deletions
diff --git a/src/common/crypto.c b/src/common/crypto.c index 1a1dad616c..23e2a429f5 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -2056,6 +2056,21 @@ crypto_rand_uint64(uint64_t max) } } +/** Return a pseudorandom double d, chosen uniformly from the range + * 0.0 <= d < 1.0. + */ +double +crypto_rand_double(void) +{ + /* We just use an unsigned int here; we don't really care about getting + * more than 32 bits of resolution */ + unsigned int uint; + do { + crypto_rand((char*)&uint, sizeof(uint)); + } while (uint == UINT_MAX); + return ((double)uint) / (double)UINT_MAX; +} + /** Generate and return a new random hostname starting with <b>prefix</b>, * ending with <b>suffix</b>, and containing no less than * <b>min_rand_len</b> and no more than <b>max_rand_len</b> random base32 diff --git a/src/common/crypto.h b/src/common/crypto.h index 1b004dd4b8..a30e5bcbae 100644 --- a/src/common/crypto.h +++ b/src/common/crypto.h @@ -210,6 +210,7 @@ int crypto_seed_rng(int startup); int crypto_rand(char *to, size_t n); int crypto_rand_int(unsigned int max); uint64_t crypto_rand_uint64(uint64_t max); +double crypto_rand_double(void); char *crypto_random_hostname(int min_rand_len, int max_rand_len, const char *prefix, const char *suffix); |