函数根据给定的概率生成1或0 [英] function to generate 1 or 0 according to a given probability

查看:168
本文介绍了函数根据给定的概率生成1或0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我试图创建一个生成1或0的函数,其中生成1的

概率等于X(传递给

函数作为参数)。


任何想法?

感谢期待,

Mayank

解决方案

Mayank Kaushik写道:


大家好,我试图创建一个生成1或0的函数,生成1的概率等于X(传递给
函数为一个参数)。

任何想法?
感谢期待,




/ * BEGIN new.c * /


#include< stdio.h>

#include< stdlib.h>


int X_prob(double x );


int main(无效)

{

无符号数;


printf(" \ n0.95");

for(count = 0; c ount!= 70; ++ count){

printf("%d",X_prob(0.95));

}

printf (\ n\\\
0.50");

for(count = 0;伯爵!= 70; ++ count){

printf("%d",X_prob(0.5));

}

printf(" \ n\\\
0.05");

for(count = 0; count!= 70; ++ count){

printf("%d", X_prob(0.05));

}

putchar(''\ n'');

返回0;

}


int X_prob(双x)

{

返回RAND_MAX * x> = rand();

}


/ * END new.c * /

-

pete


" Mayank Kaushik" < PR *************** @ yahoo.com>写在

新闻:11 ********************* @ f14g2000cwb.googlegro ups.com:

我试图创建一个生成1或0的函数,生成1的概率等于X(作为参数传递给
函数)。 / blockquote>


假设rand生成均匀分布的值,这是微不足道的。我怀疑这个问题在clc中可能是偏离主题的:comp.programming

可能是一个更好的地方可以提出这个问题。


#include< stdio.h>

#include< stdlib.h>

#include< time.h>


int成功(双概率){

返回概率> =((双)rand())/ RAND_MAX;

}


int main(void){

int i;

srand(time(NULL));


for(i = 0; i< 10; ++ i){

printf("%s \ n",success(0.38)?" success""失败);

}


返回0;

}


思南

-

A. Sinan Unur< 1u ** @ llenroc.ude.invalid>

(反转每个组件并删除.invalid对于电子邮件地址)




A. Sinan Unur写道:

" Mayank Kaushik" < PR *************** @ yahoo.com>在
新闻中写道:11 ********************* @ f14g2000cwb.googlegro ups.com:

我试图创建一个生成1或0的函数,生成1的概率等于X(作为参数传递给
函数)。



假设rand生成均匀分布的值,这是微不足道的。我怀疑这个问题在clc中可能是偏离主题的:comp.programming
可能是提出这个问题的更好的地方。

#include< stdio.h>
#include< stdlib.h>
#include< time.h>

int success(double prob){
return prob> =(( double)rand())/ RAND_MAX;
}




如果prob == 0.0并且对rand()的调用返回0怎么办?


hi everyone,

im trying to create a function that generates a 1 or a 0, with the
probability of a 1 being generated equal to X (which is passed to the
function as a parameter).

any ideas?
thanks in anticipation,
Mayank

解决方案

Mayank Kaushik wrote:


hi everyone,

im trying to create a function that generates a 1 or a 0, with the
probability of a 1 being generated equal to X (which is passed to the
function as a parameter).

any ideas?
thanks in anticipation,



/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

int X_prob(double x);

int main(void)
{
unsigned count;

printf("\n0.95 ");
for (count = 0; count != 70; ++count) {
printf("%d", X_prob(0.95));
}
printf("\n\n0.50 ");
for (count = 0; count != 70; ++count) {
printf("%d", X_prob(0.5));
}
printf("\n\n0.05 ");
for (count = 0; count != 70; ++count) {
printf("%d", X_prob(0.05));
}
putchar(''\n'');
return 0;
}

int X_prob(double x)
{
return RAND_MAX * x >= rand();
}

/* END new.c */
--
pete


"Mayank Kaushik" <pr***************@yahoo.com> wrote in
news:11*********************@f14g2000cwb.googlegro ups.com:

im trying to create a function that generates a 1 or a 0, with the
probability of a 1 being generated equal to X (which is passed to the
function as a parameter).



Assuming rand generates uniformly distributed values, this is trivial. I
suspect that the question might be off-topic in clc: comp.programming
might be a better place to ask this question.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int success(double prob) {
return prob >= ((double) rand())/RAND_MAX;
}

int main(void) {
int i;
srand(time(NULL));

for(i = 0; i < 10; ++i) {
printf("%s\n", success(0.38) ? "success" : "failure");
}

return 0;
}

Sinan
--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)



A. Sinan Unur wrote:

"Mayank Kaushik" <pr***************@yahoo.com> wrote in
news:11*********************@f14g2000cwb.googlegro ups.com:

im trying to create a function that generates a 1 or a 0, with the
probability of a 1 being generated equal to X (which is passed to the
function as a parameter).



Assuming rand generates uniformly distributed values, this is trivial. I
suspect that the question might be off-topic in clc: comp.programming
might be a better place to ask this question.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int success(double prob) {
return prob >= ((double) rand())/RAND_MAX;
}



What if prob == 0.0 and the call to rand() returns 0?


这篇关于函数根据给定的概率生成1或0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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