在一个封闭的时间间隔内的兰特 [英] rand in a closed interval on the ints

查看:45
本文介绍了在一个封闭的时间间隔内的兰特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



#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 flat
p.d.f. . Does it look right, besides having some extraneous source? frank

推荐答案

Frank Silvermann写道:
Frank Silvermann wrote:
[我已经剪掉了95%的原始代码
帖子称为外来源。对于原始海报来说,这是一个困惑,因为它知道它是无关紧要的,并没有自己剪掉它。也许下次他会发布一个关于三行代码的问题,并将其嵌入到尤利西斯的完整文本中。

int rand_in_range(int m,int n,int o)
{
/ *种子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
[I''ve snipped away the 95% of the code that the original
post called "extraneous source." It''s a puzzlement why
the original poster, knowing that it was extraneous,
didn''t snip it himself. Maybe next time he''ll post a
question about three lines of code, and embed it in the
complete text of "Ulysses."

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 flat
p.d.f. . Does it look right, besides having some extraneous source? frank




不,它看起来不对 - 在这种情况下,看起来

不是欺骗。除了无用的第三个

参数和愚蠢的无用指针,

函数是错误的。例如,如果要求[42,42]中的值,请考虑会发生什么。




-

Eric Sosman
es*****@acm-dot-org.inva lid



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].

--
Eric Sosman
es*****@acm-dot-org.invalid


Eric Sosman写道:
Eric Sosman wrote:
Frank Silvermann写道:
Frank Silvermann wrote:
[我已经剪掉了95%的代码原来的
>帖子称为外来源。
>这是一个难题。原始海报,知道它是无关的,
>自己没有剪断它。也许下次他会发布一个
>关于三行代码的问题,并将其嵌入
> 尤利西斯的完整文本。
[削减其他95%]

我很难确定相关的内容。虽然我没有使用SWAP宏在原帖上使用SWAP宏,但我很高兴能够在周围处理预期的和非数学的反对意见 br />
被撤销。我相信这会在一个封闭的区间内给出一个伪随机的平坦的p.d.f. 。它看起来是否正确,除了有一些无关的来源?
frank
[I''ve snipped away the 95% of the code that the original
> post called "extraneous source." It''s a puzzlement why
> the original poster, knowing that it was extraneous,
> didn''t snip it himself. Maybe next time he''ll post a
> question about three lines of code, and embed it in the
> complete text of "Ulysses." [snipped away the other 95 percent]
It was hard for me to determine what was relevant. Although I didn''t
use the SWAP macro for on the original post, I was glad to have it
around to deal with the expected and unmathematical objection that they
be reversed. I believe this gives a pseudo random in a closed interval with a flat
p.d.f. . Does it look right, besides having some extraneous source?
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].




#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; < br $>
}

/ *结束来源* /

我认为这种情况越来越近了。 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 100
int 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 sometimes
hard to trim out of a function on the fly. If ITERATIONS is a positive
power of ten, then the expected value is going to be the average of the
two inputs with the decimal point moved. frank


Eric Sosman写道:
Eric Sosman wrote:
Frank Silvermann写道:
Frank Silvermann wrote:
[我已经剪掉了95%原始
[snip]的代码我相信这会在一个封闭的间隔中给出一个伪随机的平面
pdf 。它看起来是否正确,除了有一些无关的来源?
frank
[I''ve snipped away the 95% of the code that the original [snip] I believe this gives a pseudo random in a closed interval with a flat
p.d.f. . Does it look right, besides having some extraneous source?
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 ++)

{<如果(i <10)printf(return is%d \ n,tja); br />
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; < br $>
}

/ *结束来源* /

我认为这是对的。 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 100

int 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


这篇关于在一个封闭的时间间隔内的兰特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆