6-03 2,532PVs
Power Law分布随机数生成算法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <stdio.h> #include <time.h> #include <math.h> #include <stdlib.h> #define alpha 1.2 #define min 1 #define max 100 double uniform(double a,double b,long long int *seed) { // printf("seed start:%lldn", *seed); double t; // double seed = (double)s; *seed=2045*(*seed)+1; *seed=*seed-(*seed/1048576)*1048576; // printf("seed:%lldn", *seed); t=(*seed)/1048576.0; t=a+(b-a)*t; return t; } void main() { double x,y; int i,j; float n=(-alpha); long long int s; unsigned int ss; double uniform(double,double,long long int *); //printf("ÇëÊäÈëÁœžö¶ËµãµÄÖµ£šÏÈСºóŽó£©£ºn"); //scanf("%lf,%lf",&min,&max); ss = time(NULL); //printf("ss:%un", ss); srand(ss); s=(long long int)rand(); FILE *fp=fopen("powelaw.txt","w"); for (i=0;i<10;i++) { for (j=0;j<5;j++) { y= uniform(0,1,&s); //printf("%fn",y); x = pow(((pow(max,n+1) - pow(min,n+1))*y + pow(min,n+1)),(1/(n+1))); printf("x=%fn",x); fprintf(fp, "%f n", x); } } fclose(fp); } |
zipf分布随机数生成算法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
//==================================================== file = genzipf.c ===== //= Program to generate Zipf (power law) distributed random variables = //=========================================================================== //= Notes: 1) Writes to a user specified output file = //= 2) Generates user specified number of values = //= 3) Run times is same as an empirical distribution generator = //= 4) Implements p(i) = C/i^alpha for i = 1 to N where C is the = //= normalization constant (i.e., sum of p(i) = 1). = //=-------------------------------------------------------------------------= //= Example user input: = //= = //= ---------------------------------------- genzipf.c ----- = //= - Program to generate Zipf random variables - = //= -------------------------------------------------------- = //= Output file name ===================================> output.dat = //= Random number seed =================================> 1 = //= Alpha vlaue ========================================> 1.0 = //= N value ============================================> 1000 = //= Number of values to generate =======================> 5 = //= -------------------------------------------------------- = //= - Generating samples to file - = //= -------------------------------------------------------- = //= -------------------------------------------------------- = //= - Done! = //= -------------------------------------------------------- = //=-------------------------------------------------------------------------= //= Example output file ("output.dat" for above): = //= = //= 1 = //= 1 = //= 161 = //= 17 = //= 30 = //=-------------------------------------------------------------------------= //= Build: bcc32 genzipf.c = //=-------------------------------------------------------------------------= //= Execute: genzipf = //=-------------------------------------------------------------------------= //= Author: Kenneth J. Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (11/16/03) - Genesis (from genexp.c) = //=========================================================================== //----- Include files ------------------------------------------------------- #include <assert.h> // Needed for assert() macro #include <stdio.h> // Needed for printf() #include <stdlib.h> // Needed for exit() and ato*() #include <math.h> // Needed for pow() //----- Constants ----------------------------------------------------------- #define FALSE 0 // Boolean false #define TRUE 1 // Boolean true //----- Function prototypes ------------------------------------------------- int zipf(double alpha, int n); // Returns a Zipf random variable double rand_val(int seed); // Jain's RNG //===== Main program ======================================================== void main(void) { FILE *fp; // File pointer to output file char file_name[256]; // Output file name string char temp_string[256]; // Temporary string variable double alpha; // Alpha parameter double n; // N parameter int num_values; // Number of values int zipf_rv; // Zipf random variable int i; // Loop counter // Output banner printf("---------------------------------------- genzipf.c ----- n"); printf("- Program to generate Zipf random variables - n"); printf("-------------------------------------------------------- n"); // Prompt for output filename and then create/open the file printf("Output file name ===================================> "); scanf("%s", file_name); fp = fopen(file_name, "w"); if (fp == NULL) { printf("ERROR in creating output file (%s) n", file_name); exit(1); } // Prompt for random number seed and then use it printf("Random number seed (greater than 0) ================> "); scanf("%s", temp_string); rand_val((int) atoi(temp_string)); // Prompt for alpha value printf("Alpha value ========================================> "); scanf("%s", temp_string); alpha = atof(temp_string); // Prompt for N value printf("N value ============================================> "); scanf("%s", temp_string); n = atoi(temp_string); // Prompt for number of values to generate printf("Number of values to generate =======================> "); scanf("%s", temp_string); num_values = atoi(temp_string); // Output "generating" message printf("-------------------------------------------------------- n"); printf("- Generating samples to file - n"); printf("-------------------------------------------------------- n"); // Generate and output zipf random variables for (i=0; i<num_values; i++) { zipf_rv = zipf(alpha, n); fprintf(fp, "%d n", zipf_rv); } // Output "done" message and close the output file printf("-------------------------------------------------------- n"); printf("- Done! n"); printf("-------------------------------------------------------- n"); fclose(fp); } //=========================================================================== //= Function to generate Zipf (power law) distributed random variables = //= - Input: alpha and N = //= - Output: Returns with Zipf distributed random variable = //=========================================================================== int zipf(double alpha, int n) { static int first = TRUE; // Static first time flag static double c = 0; // Normalization constant double z; // Uniform random number (0 < z < 1) double sum_prob; // Sum of probabilities double zipf_value; // Computed exponential value to be returned int i; // Loop counter // Compute normalization constant on first call only if (first == TRUE) { for (i=1; i<=n; i++) c = c + (1.0 / pow((double) i, alpha)); c = 1.0 / c; first = FALSE; } // Pull a uniform random number (0 < z < 1) do { z = rand_val(0); } while ((z == 0) || (z == 1)); // Map z to the value sum_prob = 0; for (i=1; i<=n; i++) { sum_prob = sum_prob + c / pow((double) i, alpha); if (sum_prob >= z) { zipf_value = i; break; } } // Assert that zipf_value is between 1 and N assert((zipf_value >=1) && (zipf_value <= n)); return(zipf_value); } //========================================================================= //= Multiplicative LCG for generating uniform(0.0, 1.0) random numbers = //= - x_n = 7^5*x_(n-1)mod(2^31 - 1) = //= - With x seeded to 1 the 10000th x value should be 1043618065 = //= - From R. Jain, "The Art of Computer Systems Performance Analysis," = //= John Wiley & Sons, 1991. (Page 443, Figure 26.2) = //========================================================================= double rand_val(int seed) { const long a = 16807; // Multiplier const long m = 2147483647; // Modulus const long q = 127773; // m div a const long r = 2836; // m mod a static long x; // Random int value long x_div_q; // x divided by q long x_mod_q; // x modulo q long x_new; // New x value // Set the seed if argument is non-zero and then return zero if (seed > 0) { x = seed; return(0.0); } // RNG using integer arithmetic x_div_q = x / q; x_mod_q = x % q; x_new = (a * x_mod_q) - (r * x_div_q); if (x_new > 0) x = x_new; else x = x_new + m; // Return a random value between 0.0 and 1.0 return((double) x / m); } |
这两个随机数你用来干什么??
用来在网络里产生流量用的。
每次来都觉得好洋气呀,哈哈·····
不洋气了,程序员的博客都这样的。嘻嘻