Last change
on this file was
7811,
checked in by westram, 13 years ago
|
merge from dev [7748] [7749] [7750]
- comments (C→C++ style)
- fixed umlauts in TREEGEN
|
-
Property svn:eol-style set to
native
-
Property svn:keywords set to
Author Date Id Revision
|
File size:
1.3 KB
|
Line | |
---|
1 | #define __MODUL__ |
---|
2 | #include "spreadin.h" |
---|
3 | #include <stdlib.h> |
---|
4 | |
---|
5 | Spreading newSpreading(double *value, int values) |
---|
6 | { |
---|
7 | Spreading s; |
---|
8 | int v; |
---|
9 | double val = 0.0, |
---|
10 | sum = 0.0; |
---|
11 | |
---|
12 | s = malloc(sizeof(struct S_Spreading)); |
---|
13 | if (!s) outOfMemory(); |
---|
14 | |
---|
15 | assert(values>0); |
---|
16 | |
---|
17 | s->values = values; |
---|
18 | s->border = malloc(sizeof(*(s->border))*values); |
---|
19 | if (!s->border) outOfMemory(); |
---|
20 | |
---|
21 | for (v = 0; v<values; v++) sum += value[v]; // Summe bilden |
---|
22 | |
---|
23 | for (v = 0; v<values; v++) |
---|
24 | { |
---|
25 | val += value[v]; |
---|
26 | s->border[v] = (val/sum)*RAND_MAX; |
---|
27 | } |
---|
28 | |
---|
29 | s->border[values-1] = RAND_MAX; |
---|
30 | |
---|
31 | return s; |
---|
32 | } |
---|
33 | void freeSpreading(Spreading s) |
---|
34 | { |
---|
35 | free(s->border); |
---|
36 | free(s); |
---|
37 | } |
---|
38 | int spreadRand(Spreading s) |
---|
39 | { |
---|
40 | int val = rand(), |
---|
41 | l = 0, |
---|
42 | h = s->values-1, |
---|
43 | m; |
---|
44 | |
---|
45 | while (1) |
---|
46 | { |
---|
47 | m = (l+h)/2; |
---|
48 | |
---|
49 | if (val<=s->border[m]) |
---|
50 | { |
---|
51 | if (m==0 || val>s->border[m-1]) break; |
---|
52 | h = m; |
---|
53 | } |
---|
54 | else |
---|
55 | { |
---|
56 | if (m==(h-1)) |
---|
57 | { |
---|
58 | if (val<=s->border[h]) |
---|
59 | { |
---|
60 | m = h; |
---|
61 | break; |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | l = m; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | assert(m>=0 && m<s->values); |
---|
70 | assert(val<=s->border[m]); |
---|
71 | assert(m==0 || val>=s->border[m-1]); |
---|
72 | |
---|
73 | return m; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
Note: See
TracBrowser
for help on using the repository browser.