C 一个程序,用一枚硬币模拟抛硬币

C 一个程序,用一枚硬币模拟抛硬币,c,loops,random,C,Loops,Random,我没有任何代码,主要是因为我还没有开始处理这个特定的问题。这是我C编程课的家庭作业 我的教授想让我们创建一个程序,可以抛硬币(正面或反面)10000次。然而,heads元素有55%的几率发生。我们必须使用带有用户提供的seed值的随机数生成器 从概念上讲,我知道如何处理这个问题;就编码而言,我不知道。我知道如何制作抛硬币程序,但不带偏见 任何和所有的帮助都将不胜感激 我已经附上了抛硬币程序的代码。我本来打算把它作为这个新的投币计划的基础 // CoinTosser_Homework4.cpp :

我没有任何代码,主要是因为我还没有开始处理这个特定的问题。这是我C编程课的家庭作业

我的教授想让我们创建一个程序,可以抛硬币(正面或反面)10000次。然而,heads元素有55%的几率发生。我们必须使用带有用户提供的
seed
值的随机数生成器

从概念上讲,我知道如何处理这个问题;就编码而言,我不知道。我知道如何制作抛硬币程序,但不带偏见

任何和所有的帮助都将不胜感激

我已经附上了抛硬币程序的代码。我本来打算把它作为这个新的投币计划的基础

// CoinTosser_Homework4.cpp : Coin tossing program
// nxt3

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define HEADS 0
#define TAILS 1

int rand_int(int a, int b) {

return rand() % ((b - a + 1) + a);
}

int main() {

int tosses = 0, min = 0, heads = 0, tails = 0; //init tosses and number of user defined tosses

/*users input for number of tosses*/
printf("Enter number of coin tosses: ");
scanf("%i", &min);

while (tosses < min) {
    tosses++;
    if (rand_int(HEADS, TAILS) == HEADS)
        heads++;
    else
        tails++;
}

//prints results of tosses
printf("Number of heads: %i\n", heads);
printf("Number of tails: %i\n", tails);

return 0;
}
//投币者家庭作业4.cpp:投币程序
//nxt3
#包括“stdafx.h”
#包括
#包括
#定义头0
#定义尾部1
整数和整数(整数a,整数b){
返回rand()%((b-a+1)+a);
}
int main(){
int-throups=0,min=0,heads=0,tails=0;//初始throups和用户定义的throups数量
/*用户输入投掷次数*/
printf(“输入投币次数:”);
scanf(“%i”、&min);
while(投掷
用传统的方式:

rand() % 100 + 1;
为您提供范围为1到100(含)的数字。如果该数字小于或等于55,则为人头

但请注意,此生成器是有偏差的,除非生成器的周期是100的倍数(可能不是)。你真正应该做的是使用

(int)(100.0 * rand() / (RAND_MAX + 1.0)) + 1

100.0
还避免了整数除法。

采用老式方法:

rand() % 100 + 1;
为您提供范围为1到100(含)的数字。如果该数字小于或等于55,则为人头

但请注意,此生成器是有偏差的,除非生成器的周期是100的倍数(可能不是)。你真正应该做的是使用

(int)(100.0 * rand() / (RAND_MAX + 1.0)) + 1

100.0
还避免了整数除法。

采用老式方法:

rand() % 100 + 1;
为您提供范围为1到100(含)的数字。如果该数字小于或等于55,则为人头

但请注意,此生成器是有偏差的,除非生成器的周期是100的倍数(可能不是)。你真正应该做的是使用

(int)(100.0 * rand() / (RAND_MAX + 1.0)) + 1

100.0
还避免了整数除法。

采用老式方法:

rand() % 100 + 1;
为您提供范围为1到100(含)的数字。如果该数字小于或等于55,则为人头

但请注意,此生成器是有偏差的,除非生成器的周期是100的倍数(可能不是)。你真正应该做的是使用

(int)(100.0 * rand() / (RAND_MAX + 1.0)) + 1

< > >代码> 100 /代码>也回避整数除法。

我忘记了C的语法,但我可以提供C++的解决方案。从这里你可以看到算法

#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    int heads=0, tails=0;
    srand(time(NULL));
    number = rand() % 100 + 1;  //Generate random number 1 to 100
          if (number <= 55) //55% chance
                heads++; //This is head
          else
                tails++; //This is tail
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int头=0,尾=0;
srand(时间(空));
number=rand()%100+1;//生成随机数1到100

如果(数< p>我忘记了C的语法,但我可以提供C++的解决方案。从这里你可以看到算法。< /P>
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    int heads=0, tails=0;
    srand(time(NULL));
    number = rand() % 100 + 1;  //Generate random number 1 to 100
          if (number <= 55) //55% chance
                heads++; //This is head
          else
                tails++; //This is tail
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int头=0,尾=0;
srand(时间(空));
number=rand()%100+1;//生成随机数1到100

如果(数< p>我忘记了C的语法,但我可以提供C++的解决方案。从这里你可以看到算法。< /P>
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    int heads=0, tails=0;
    srand(time(NULL));
    number = rand() % 100 + 1;  //Generate random number 1 to 100
          if (number <= 55) //55% chance
                heads++; //This is head
          else
                tails++; //This is tail
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int头=0,尾=0;
srand(时间(空));
number=rand()%100+1;//生成随机数1到100

如果(数< p>我忘记了C的语法,但我可以提供C++的解决方案。从这里你可以看到算法。< /P>
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    int heads=0, tails=0;
    srand(time(NULL));
    number = rand() % 100 + 1;  //Generate random number 1 to 100
          if (number <= 55) //55% chance
                heads++; //This is head
          else
                tails++; //This is tail
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int头=0,尾=0;
srand(时间(空));
number=rand()%100+1;//生成随机数1到100


如果(number)生成一个介于0..99而不是0..1之间的随机数,则主编码oops是
rand()%((b-a+1)+a)
。您想
rand()%(b-a+1)+a;
生成一个介于0..99而不是0..1之间的随机数,则主编码oops是
rand()%((b-a+1)+a)
。认为您想要
rand()%(b-a+1)+a;
生成一个介于0..99而不是0..1之间的随机数。看起来主编码oops是
rand()%((b-a+1)+a)
。认为您想要
rand()%(b-a+1)+a;
生成一个介于0..99而不是0..1之间的随机数。看起来主编码oops是
rand()%((b-a+1)+a)
。我想你想要
rand()%(b-a+1)+a;
伙计。我没想过。我有点不好意思,我甚至贴了这个,哈哈。我很感激。:)我想你想要
(int)(100.0*rand()/(rand\u MAX+1.0))+1
来平衡事情。否则代码可能会生成101。@chux:good spot:needed,因为
rand()
可以画
RAND\u MAX
。我已经修改了。谢谢。建议
RAND\u MAX+1.0
RAND\u MAX+1
相比
RAND\u MAX+1
可能会溢出。@chux:另一个好观点。我没有解释就添加了-可能超出了问题的范围。我们应该联合发布吗?伙计。我没有想过。我甚至有点尴尬我想你想要
(int)(100.0*rand()/(rand\u MAX+1.0))+1
来平衡事情。否则代码可能会生成101。@chux:好点:因为
rand()
可以绘制
rand\u MAX
。我已经修改了。谢谢。建议