summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <sandmann@daimi.au.dk>2009-08-30 09:47:24 -0400
committerSøren Sandmann Pedersen <sandmann@daimi.au.dk>2009-08-30 09:47:24 -0400
commit7f6630c220a3cea3b594731e0a8e1e34da030cb9 (patch)
treecaf0ba27d98e5dd6fdbee64186225e6ab748b3e3
parent7abd684aeefc6c5a03dcfd779333081be88aaacc (diff)
Formatting
-rw-r--r--random.c60
1 files changed, 32 insertions, 28 deletions
diff --git a/random.c b/random.c
index cc84023..3997bcb 100644
--- a/random.c
+++ b/random.c
@@ -89,14 +89,16 @@ nul_rand_set_seed (rand_t* rand, uint32_t seed)
{
g_return_if_fail (rand != NULL);
- /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
- /* In the previous version (see above), MSBs of the */
- /* seed affect only MSBs of the array mt[]. */
-
- rand->mt[0]= seed;
- for (rand->mti=1; rand->mti<N; rand->mti++)
- rand->mt[rand->mti] = 1812433253UL *
- (rand->mt[rand->mti-1] ^ (rand->mt[rand->mti-1] >> 30)) + rand->mti;
+ /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
+ /* In the previous version (see above), MSBs of the */
+ /* seed affect only MSBs of the array mt[]. */
+
+ rand->mt[0]= seed;
+ for (rand->mti=1; rand->mti<N; rand->mti++)
+ {
+ rand->mt[rand->mti] = 1812433253UL *
+ (rand->mt[rand->mti-1] ^ (rand->mt[rand->mti-1] >> 30)) + rand->mti;
+ }
}
/**
@@ -188,7 +190,9 @@ static rand_t*
nul_rand_new_with_seed_array (const uint32_t *seed, guint seed_length)
{
rand_t *rand = g_new0 (rand_t, 1);
+
nul_rand_set_seed_array (rand, seed, seed_length);
+
return rand;
}
@@ -326,29 +330,29 @@ nul_rand_int_range (rand_t* rand, int32_t begin, int32_t end)
g_return_val_if_fail (rand != NULL, begin);
g_return_val_if_fail (end > begin, begin);
- if (dist == 0)
- random = 0;
- else
+ if (dist == 0)
+ random = 0;
+ else
+ {
+ /* maxvalue is set to the predecessor of the greatest
+ * multiple of dist less or equal 2^32. */
+ uint32_t maxvalue;
+ if (dist <= 0x80000000u) /* 2^31 */
{
- /* maxvalue is set to the predecessor of the greatest
- * multiple of dist less or equal 2^32. */
- uint32_t maxvalue;
- if (dist <= 0x80000000u) /* 2^31 */
- {
- /* maxvalue = 2^32 - 1 - (2^32 % dist) */
- uint32_t leftover = (0x80000000u % dist) * 2;
- if (leftover >= dist) leftover -= dist;
- maxvalue = 0xffffffffu - leftover;
- }
- else
- maxvalue = dist - 1;
+ /* maxvalue = 2^32 - 1 - (2^32 % dist) */
+ uint32_t leftover = (0x80000000u % dist) * 2;
+ if (leftover >= dist) leftover -= dist;
+ maxvalue = 0xffffffffu - leftover;
+ }
+ else
+ maxvalue = dist - 1;
- do
- random = nul_rand_int (rand);
- while (random > maxvalue);
+ do
+ random = nul_rand_int (rand);
+ while (random > maxvalue);
- random %= dist;
- }
+ random %= dist;
+ }
return begin + random;
}