Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

random.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- 00002 @COPYRIGHT : 00003 Copyright 1993,1994,1995 David MacDonald, 00004 McConnell Brain Imaging Centre, 00005 Montreal Neurological Institute, McGill University. 00006 Permission to use, copy, modify, and distribute this 00007 software and its documentation for any purpose and without 00008 fee is hereby granted, provided that the above copyright 00009 notice appear in all copies. The author and McGill University 00010 make no representations about the suitability of this 00011 software for any purpose. It is provided "as is" without 00012 express or implied warranty. 00013 ---------------------------------------------------------------------------- */ 00014 00015 #include <volume_io/internal_volume_io.h> 00016 #include <bicpl/prog_utils.h> 00017 #include <sys/time.h> 00018 00019 #ifndef lint 00020 static char rcsid[] = "$Header: /software/source//libraries/bicpl/Prog_utils/random.c,v 1.13 2001/02/05 22:03:48 stever Exp $"; 00021 #endif 00022 00023 #define MAX_RAND 2147483648.0 00024 00025 private BOOLEAN initialized = FALSE; 00026 00027 /* ----------------------------- MNI Header ----------------------------------- 00028 @NAME : set_random_seed 00029 @INPUT : 00030 @OUTPUT : 00031 @RETURNS : 00032 @DESCRIPTION: Sets the random seed for the generator 00033 @METHOD : 00034 @GLOBALS : 00035 @CALLS : 00036 @CREATED : 1993 David MacDonald 00037 @MODIFIED : 00038 ---------------------------------------------------------------------------- */ 00039 00040 public void set_random_seed( int seed ) 00041 { 00042 (void) srandom( (unsigned int) seed ); 00043 initialized = TRUE; 00044 } 00045 00046 /* ----------------------------- MNI Header ----------------------------------- 00047 @NAME : check_initialized 00048 @INPUT : 00049 @OUTPUT : 00050 @RETURNS : 00051 @DESCRIPTION: Ensures that the random seed has been set. 00052 @METHOD : 00053 @GLOBALS : 00054 @CALLS : 00055 @CREATED : 1993 David MacDonald 00056 @MODIFIED : 00057 ---------------------------------------------------------------------------- */ 00058 00059 private void check_initialized( void ) 00060 { 00061 struct timeval t; 00062 int seed; 00063 00064 if( !initialized ) 00065 { 00066 (void) gettimeofday( &t, (struct timezone *) 0 ); 00067 00068 seed = (int) t.tv_usec; 00069 00070 set_random_seed( seed ); 00071 } 00072 } 00073 00074 /* ----------------------------- MNI Header ----------------------------------- 00075 @NAME : get_random 00076 @INPUT : 00077 @OUTPUT : 00078 @RETURNS : int 00079 @DESCRIPTION: Gets a random number from 0 to 2^30 00080 @METHOD : 00081 @GLOBALS : 00082 @CALLS : 00083 @CREATED : 1993 David MacDonald 00084 @MODIFIED : 00085 ---------------------------------------------------------------------------- */ 00086 00087 private int get_random( void ) 00088 { 00089 check_initialized(); 00090 00091 return( (int) random() ); 00092 } 00093 00094 /* ----------------------------- MNI Header ----------------------------------- 00095 @NAME : get_random_int 00096 @INPUT : n 00097 @OUTPUT : 00098 @RETURNS : int 00099 @DESCRIPTION: Returns a random number from 0 to n-1. 00100 @METHOD : 00101 @GLOBALS : 00102 @CALLS : 00103 @CREATED : 1993 David MacDonald 00104 @MODIFIED : 00105 ---------------------------------------------------------------------------- */ 00106 00107 public int get_random_int( int n ) 00108 { 00109 return( get_random() % n ); 00110 } 00111 00112 /* ----------------------------- MNI Header ----------------------------------- 00113 @NAME : get_random_0_to_1 00114 @INPUT : 00115 @OUTPUT : 00116 @RETURNS : random number 00117 @DESCRIPTION: Returns a random number greater than or equal to 0 and and less 00118 : than 1. 00119 @METHOD : 00120 @GLOBALS : 00121 @CALLS : 00122 @CREATED : 1993 David MacDonald 00123 @MODIFIED : 00124 ---------------------------------------------------------------------------- */ 00125 00126 public Real get_random_0_to_1( void ) 00127 { 00128 return( (Real) get_random() / MAX_RAND ); 00129 }

Generated on Wed Jul 28 09:10:57 2004 for BICPL by doxygen 1.3.7