first c conc distribution implementation (slow and commented out)
[physik/morpheus.git] / random.c
1 /*
2  * random.c - functions to get random values
3  *
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9
10 #include "defines.h"
11
12 /* global and extern variables */
13 extern u32 gr;
14 extern int random_fd;
15
16 /* return random integer between 0 - max-1 */
17 u32 rand_get(u32 max) 
18 {
19  u32 rand_int;
20  if(read(random_fd,&rand_int,4)!=4)
21  {
22   puts("failed reading 4 bytes of random data");
23   return -23;
24  }
25  /* cells numbered 0...max-1 */
26  return((u32)(rand_int*(max*1.0/URAND_MAX)));
27 }
28
29 /* get z value (linear growth of probability with depths) */
30 u32 rand_get_lgp(int slope_nel,int start_nel,u32 z_max)
31 {
32  int z;
33  u32 i,weighted_sum_z;
34  weighted_sum_z=z_max*start_nel+slope_nel*gr;
35  z=rand_get(weighted_sum_z)+1; /* +1 as rand_get returns values 0...max-1 */
36  for(i=1;;i++) {
37   z-=(start_nel+i*slope_nel);
38   if(z<=0) break;
39  }
40  return(i-1); /* return values 0...z_cell-1 */
41 }
42
43