From: hackbard Date: Thu, 20 Mar 2003 20:42:10 +0000 (+0000) Subject: base random stuff fixed X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ec676caee02b75707e472b0294e8be51aab4c42;p=physik%2Fmorpheus.git base random stuff fixed --- diff --git a/defines.h b/defines.h index 2e399a4..58e758d 100644 --- a/defines.h +++ b/defines.h @@ -9,8 +9,10 @@ #define DEFAULT_Y_SEG 50 #define DEFAULT_Z_SEG 100 -#define DEFAULT_SLOPE_CC 1 -#define DEFAULT_START_CC 0 +#define DEFAULT_SLOPE_NEL 1 +#define DEFAULT_START_NEL 0 + +#define DEFAULT_STEPS 5000 /* program defines, dont touch ;) */ @@ -20,5 +22,24 @@ #define DEBUG 0 #endif +#define URAND_MAX 0xffffffff + +typedef unsigned int u32; +typedef long long unsigned int u64; + #define printfd(fmt,args...) if(DEBUG) printf(fmt,##args); +/* segment struct + * + * not used by now + */ +struct __segment +{ + u32 status; /* C concentration */ +} + +/* masks for u32 segments */ +#define AMORPH 0x00000001 +#define CRYSTAL 0x00000000 +#define C_CONC_MASK 0xfffffffe + diff --git a/main.c b/main.c index cf2a703..49be0ef 100644 --- a/main.c +++ b/main.c @@ -13,35 +13,60 @@ #include #include +#include +#include +#include + #include "defines.h" /* global variables */ -int sum_z_segments; +u32 sum_z_segments; +int random_fd; /* /dev/urandom file descriptor */ int usage() { puts("usage:"); puts("-h: help"); - puts("-a \t slope of C concentration with depth (default 1)"); - puts("-c \t concentration at depths 0 (default 0)"); + puts("-a \t slope of nuclear energy loss (default 1)"); + puts("-c \t nuclear enery loss at depths 0 (default 0)"); puts("-x \t # x segments (default 50)"); puts("-y \t # y segments (default 50)"); puts("-z \t # z segments (default 100)"); + puts("-s \t # steps to calculate (default 5000"); return -23; } -int main(int argc,char **argv) { - int x_seg,y_seg,z_seg; /* amount of segments */ - int x,y,z; /* segments */ +int make_amorph(u32 *segment) +{ + *segment=*segment|AMORPH + return 23; +} + +/* look at segment ... */ +int process_segment(u32 *segment) +{ + /* tag it ... testing! */ + make_amorph(segment); + + return 23; +} + +int main(int argc,char **argv) +{ + u32 x_seg,y_seg,z_seg; /* amount of segments */ + u32 x,y,z; /* segments */ int i; /* for counting */ - int slope_cc,start_cc; /* C concentration: slope, c(0) */ + int slope_nel,start_nel; /* nuclear energy loss: slope, constant */ + int steps; /* # steps */ + void *segments_p; /* default values */ - x_seg=DEFAULT_X_SEG-1; - y_seg=DEFAULT_Y_SEG-1; - z_seg=DEFAULT_Z_SEG-1; - slope_cc=DEFAULT_SLOPE_CC; - start_cc=DEFAULT_START_CC; + x_seg=DEFAULT_X_SEG; + y_seg=DEFAULT_Y_SEG; + z_seg=DEFAULT_Z_SEG; + slope_nel=DEFAULT_SLOPE_NEL; + start_nel=DEFAULT_START_NEL; + steps=DEFAULT_STEPS; /* parse command args */ for(i=1;i %d\n",sum_z_segments); /* testing ... */ - for(i=0;i<100;i++) + /* allocate segments */ + if((segments_p=malloc(x_seg*y_seg*z_seg*sizeof(u32)))==NULL) + { + puts("failed allocating memory for segments\n"); + return -23; + } + memset(segments_p,0,x_seg*y_seg*z_seg*sizeof(u32)); + + /* main routine */ + for(i=0;i #include +#include #include "defines.h" -extern int sum_z_segments; +/* global & extern variables */ +extern u32 sum_z_segments; +extern int random_fd; /* return random integer between 0 - max */ -int rand_get(int max) { - return((int) (rand() * (max*1.0/RAND_MAX)) ); +u32 rand_get(u32 max) +{ + u32 rand_int; + if(read(random_fd,&rand_int,4)!=4) + { + puts("failed reading 4 bytes of random data"); + return -23; + } + /* segments numbered 0...max-1 */ + return((u32)(rand_int*(max*1.0/URAND_MAX))); } /* get z value (linear growth of probability with depths) */ -int rand_get_lgp(int slope_cc,int start_cc) { - int z,i; - z=rand_get(sum_z_segments); - for(i=0;;i++) { - z-=i*slope_cc; +u32 rand_get_lgp(int slope_nel,int start_nel) { + int z; + u32 i; + z=rand_get(sum_z_segments)+1; /* +1 as rand_get returns values 0...max-1 */ + for(i=1;;i++) { + z-=(start_nel+i*slope_nel); if(z<=0) break; } - return i; + return(i-1); /* return values 0...z_seg-1 */ }