本文介绍了在一个封闭的时间间隔内的兰特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! #include< stdio.h> #include< stdlib.h> #include< time.h> ; #include< math.h> #define MIN_WORD_LENGTH 9 #define MAX_WORD_LENGTH 15 #define SWAP(m,n)(tmp =(m),(m)=(n),(n)= tmp) void permute(char *, int); int * partition(int,int); int rand_in_range(int,int,int); int main(void) { char p [] =" abcdefghijklmnopqrstuvwxyz" ;; char q [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;; char r [] =" 0123456789" ;; int a [3],m,tja,i,j; / *所有声明主要需要在这里北部* / a [0] = sizeof(p) - 1; a [1] = sizeof(q ) - 1; a [2] = sizeof(r) - 1; / *计算字母大小* / m = 26 + 26 + 10; printf(tja%d%d%d%d \ n,a [0],a [1],a [2],m); / *种子计时器* / srand(时间(NULL)); / *使用硬编码调用rand * / tja = rand_in_range(3,7,7); printf(tja is%d \ n,tja); / *叫兰特一万次* / j = 0; for(i = 0;我< 10000; i ++) { tja = rand_in_range(3,7,7); j + = tja; } printf(" j is%d \ nn",j); 返回0; } void permute(char * m,int n) {; } int * partition(int m,int n) {; 返回NULL; } int rand_in_range(int m,int n,int o) { / * major srand in main * / / * [m,n]是范围* / int t,top,diff; diff = n - m; o = 0 *(int)(ace in \0); top = RAND_MAX - (RAND_MAX%diff); / * control * / do t = rand(); while(t> top); o = t%(diff + 1); 返回o; } / * end source * / 我相信这会在一个封闭的区间内给出一个伪随机的平面 pdf 。看起来是否正确,除了有一些无关的来源? frank#include <stdio.h>#include <stdlib.h>#include <time.h>#include <math.h>#define MIN_WORD_LENGTH 9#define MAX_WORD_LENGTH 15#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)void permute(char *, int);int * partition(int, int);int rand_in_range(int, int, int);int main(void){char p[] = "abcdefghijklmnopqrstuvwxyz";char q[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";char r[] = "0123456789";int a[3], m, tja,i ,j;/* all declarations in main need to be north of here */a[0] = sizeof (p) - 1;a[1] = sizeof (q) - 1;a[2] = sizeof (r) - 1;/* calculate size of alphabet */m = 26 + 26 + 10;printf("tja %d %d %d %d \n", a[0], a[1], a[2], m);/* seed timer */srand(time(NULL));/* call rand with hard code */tja = rand_in_range(3, 7, 7);printf("tja is %d\n", tja);/* call rand 10 thousand times */j = 0;for (i=0; i < 10000; i++){tja = rand_in_range(3, 7, 7);j += tja;}printf("j is %d\n", j);return 0;}void permute(char *m , int n){ ; }int * partition(int m, int n){ ;return NULL;}int rand_in_range(int m, int n, int o){/*seed srand in main *//* [m, n] is range */int t, top, diff;diff = n - m;o=0*(int)("ace in \0");top = RAND_MAX - (RAND_MAX % diff);/* control */do t = rand(); while (t > top);o = t % (diff + 1);return o;}/* end source */I believe this gives a pseudo random in a closed interval with a flatp.d.f. . Does it look right, besides having some extraneous source? frank推荐答案 不,它看起来不对 - 在这种情况下,看起来 不是欺骗。除了无用的第三个 参数和愚蠢的无用指针, 函数是错误的。例如,如果要求[42,42]中的值,请考虑会发生什么。 。 - Eric Sosman es*****@acm-dot-org.inva lidNo, it does not look right -- and in this case, looksare not deceiving. Quite aside from the useless thirdargument and the silly dabbling with a useless pointer,the function is wrong. For example, consider what happensif it is asked for a value in [42,42].--Eric Sosman es*****@acm-dot-org.invalid 不,它看起来不对 - 在这种情况下,看起来不是在欺骗。除了无用的第三个争论和愚蠢的无用指针之外,功能是错误的。例如,如果在[42,42]中要求输入值,请考虑会发生什么。 No, it does not look right -- and in this case, looks are not deceiving. Quite aside from the useless third argument and the silly dabbling with a useless pointer, the function is wrong. For example, consider what happens if it is asked for a value in [42,42]. #include< stdio.h> #include< stdlib.h> #include< time.h> #include< math.h> #define SWAP(m,n)(tmp =(m),(m)=(n),(n)= tmp) #define ITERATIONS 100 int rand_in_range(int,int); int main(无效) { int tja,i, j,tmp,m,n; 浮动亩; / *所有声明主要需要在这里北方* / / *种子计时器* / srand(时间(NULL)); / * call rand * / j = 0; m = 40; n = 3; if(m> n)SWAP(m,n ); printf(范围是[%d,%d] \ n,m,n); mu =(浮动)ITERATIONS *(( float)m +(float)(m - n)/(float)2); printf(mu is%f\ n,mu); for(i = 0; i< ITERATIONS; i ++) { tja = rand_in_range(m,n); if(i< 10)printf(return is%d \ n,tja); j + = tja; } printf(" ; j是%d \ n",j); 返回0; } int rand_in_range(int m,int n) { / *主要种子srand * / / * [m,n]是范围* / int t,top,diff,o,tmp; if(m == n)返回m; if(m > n)SWAP(m,n); diff = n - m; top = RAND_MAX - (RAND_MAX%diff); / * control * / do t = rand(); while(t> top); o = t%(diff + 1); o = o + m; return o; } / *结束来源* / 我认为这种情况越来越近了。 mu fiasco显示了为什么它有时很难在动态中删除功能。如果ITERATIONS是10的正值b / b,那么预期的值将是两个输入的小数点移动的平均值。 frank#include <stdio.h>#include <stdlib.h>#include <time.h>#include <math.h>#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)#define ITERATIONS 100int rand_in_range(int, int);int main(void){int tja, i , j, tmp, m, n;float mu;/* all declarations in main need to be north of here *//* seed timer */srand(time(NULL));/* call rand */j = 0;m = 40;n = 3;if (m > n) SWAP(m, n);printf("range is [%d, %d]\n", m, n);mu = (float)ITERATIONS *((float)m +(float)(m - n) /(float)2);printf("mu is %f\n", mu);for (i=0; i < ITERATIONS; i++){tja = rand_in_range(m, n);if (i < 10) printf("return is %d\n", tja);j += tja;}printf("j is %d\n", j);return 0;}int rand_in_range(int m, int n){/*seed srand in main *//* [m, n] is range */int t, top, diff, o, tmp;if (m == n) return m;if (m > n) SWAP(m, n);diff = n - m;top = RAND_MAX - (RAND_MAX % diff);/* control */do t = rand(); while (t > top);o = t % (diff + 1) ;o = o + m;return o;}/* end source */I think this is getting closer. The mu fiasco shows why it''s sometimeshard to trim out of a function on the fly. If ITERATIONS is a positivepower of ten, then the expected value is going to be the average of thetwo inputs with the decimal point moved. frank 不,它看起来不对 - 在这种情况下,看起来不是在欺骗。除了无用的第三个争论和愚蠢的无用指针之外,功能是错误的。例如,如果在[42,42]中要求输入值,请考虑会发生什么。 No, it does not look right -- and in this case, looks are not deceiving. Quite aside from the useless third argument and the silly dabbling with a useless pointer, the function is wrong. For example, consider what happens if it is asked for a value in [42,42]. / * rand_in_range.c:贡献者:通常的嫌疑人in clc * / #include< stdio.h> #include< stdlib.h> #include < time.h> #include< math.h> #define SWAP(m,n)(tmp =(m),(m)=( n),(n)= tmp) #define ITERATIONS 100 int rand_in_range(int,int); int main(无效) { int tja,i,j,m,n; / *主要声明需要在这里的北方* / / *种子计时器* / srand(时间(NULL)); / * call rand和计数返回* / j = 0; m = 8; n = 3; printf("范围是[%d,%d] \ n",m,n); for(i = 0; i< ITERATIONS; i ++) { j + = tja; } printf(" total is%d \ nn,",j); 返回0; } int rand_in_range(int m,int n) { / * seed srand in main * / / * [m,n]是范围* / int t,top,diff,p,tmp; 如果(m == n)返回m; if(m> n)SWAP(m,n); diff = n - m; top = RAND_MAX - (RAND_MAX%(diff + 1)+ 1); / * control * / do t = rand(); while(t> top); p = t%(diff + 1); p = p + m; 返回p; } / *结束来源* / 我认为这是对的。 frank/* rand_in_range.c : contributors: usual suspects in clc */#include <stdio.h>#include <stdlib.h>#include <time.h>#include <math.h>#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)#define ITERATIONS 100int rand_in_range(int, int);int main(void){int tja, i , j, m, n;/* all declarations in main need to be north of here *//* seed timer */srand(time(NULL));/* call rand and count returns */j = 0;m = 8;n = 3;printf("range is [%d, %d]\n", m, n);for (i=0; i < ITERATIONS; i++){tja = rand_in_range(m, n);if (i < 10) printf("return is %d\n", tja);j += tja;}printf("total is %d\n", j);return 0;}int rand_in_range(int m, int n){/*seed srand in main *//* [m, n] is range */int t, top, diff, p, tmp;if (m == n) return m;if (m > n) SWAP(m, n);diff = n - m;top = RAND_MAX - (RAND_MAX % (diff + 1) + 1);/* control */do t = rand(); while (t > top);p = t % (diff + 1) ;p = p + m;return p;}/* end source */I think this is right. frank 这篇关于在一个封闭的时间间隔内的兰特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!