From: Florian Forster Date: Thu, 12 Mar 2009 17:13:40 +0000 (+0100) Subject: src/sn-evolution2.c: Clean up the mutation probability a bit. X-Git-Tag: v1.0.0~73 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=417408db1c8c35631b11973190179d0b8732d66f;p=sort-networks.git src/sn-evolution2.c: Clean up the mutation probability a bit. --- diff --git a/src/sn-evolution2.c b/src/sn-evolution2.c index 62a0969..907c05f 100644 --- a/src/sn-evolution2.c +++ b/src/sn-evolution2.c @@ -265,22 +265,20 @@ static sn_comparator_t get_random_comparator (void) /* {{{ */ static int mutate_network (sn_comparator_t *comparators, int comparators_num) { static int old_comparators_num = -1; - static int cut_off = 1000000; + static double mutate_probability = 0.0; int i; if (old_comparators_num != comparators_num) { - double p; - - p = powl (0.5, (1.0 / ((double) comparators_num))); - cut_off = (int) (((double) 1000000) * p); - + /* Probability that NO mutation takes place: 1/3 */ + mutate_probability = powl (1.0 / 3.0, (1.0 / ((double) comparators_num))); old_comparators_num = comparators_num; } + /* `mutate_probability' actually holds 1-p, i. e. it is close to one */ for (i = 0; i < comparators_num; i++) - if (sn_bounded_random (0, 1000000) >= cut_off) + if (sn_double_random () >= mutate_probability) comparators[i] = get_random_comparator (); return (0); diff --git a/src/sn_random.c b/src/sn_random.c index 841b6e4..1c885aa 100644 --- a/src/sn_random.c +++ b/src/sn_random.c @@ -144,4 +144,9 @@ int sn_bounded_random (int min, int max) return (rand); } /* int sn_bounded_random */ +double sn_double_random (void) +{ + return (((double) sn_random ()) / (((double) RAND_MAX) + 1.0)); +} /* double sn_double_random */ + /* vim: set shiftwidth=2 softtabstop=2 : */ diff --git a/src/sn_random.h b/src/sn_random.h index 5143264..ed89cc7 100644 --- a/src/sn_random.h +++ b/src/sn_random.h @@ -26,5 +26,6 @@ int sn_random (void); int sn_true_random (void); int sn_bounded_random (int min, int max); +double sn_double_random (void); #endif /* SN_RANDOM_H */