我想在输入一个选择后~~得到不同概率的答案
如果:
......
int a,b,c;
b=100
c=50
cout<<.......;
cin>>a;
if(a==1)         
。//我想在这时得到不同的结果~分别为b和c~应该插入什么才能得出分别为50%的b和c输出

解决方案 »

  1.   

    srand((unsigned int)time(NULL));
    int i=rand()%2;if(i==0)
    {
    ....
    }
    else
    {
    ...
    }
      

  2.   

    srand((unsigned int)time(NULL));//是什么意思?不太懂~~可以按我说的例子写一个程序吗?这样比较好懂
      

  3.   

    srand 函数用来设置 随机数种子,这样可以保证每次取得的数字都不一样.
    可以看看MSDN
      

  4.   


    #include <iostream.h>
    #include <windows.h>
    #include <time.h>
    void main()
    {
    int a;
    int b;
    int c;
    b=100;
    c=50;
    cout<<".......";
    a=1;
    for(;a==1;)
    {
    cin>>a;
    if(a==1)
    {
    srand((unsigned int)time(NULL));  //初始化随机数种子,这样可以保证每次取得的数字都不一样.
    int i=rand()%2;   //所得随机数取余2,奇数i为1,偶数i为0,因为是随机的所以机率是相等的
    if(i==1)
    cout<<b<<"\n";
    else
    cout<<c<<"\n";
    }
    }
    }
      

  5.   


    #include <iostream.h>
    #include <windows.h>
    #include <time.h>
    void main()
    {
    int a;
    int b;
    int c;
    b=100;
    c=50;
    cout<<".......";
    a=1;
    for(;a==1;)
    {
    cin>>a;
    if(a==1)
    {
    srand((unsigned int)time(NULL));                     int i=rand()%2;  
    if(i==1)
    cout<<b<<"\n";
    else
    cout<<c<<"\n";
    }
    }
    }