第1个回答 2011-05-28
你可以变通一下
用随机产生0-100之间的数
然后判断小于50 一个变量++
大于50另一个变量++
当然你也可以 rand()%1 0~1 之间
那你得判断float 太麻烦了
#include "iostream"
#include "time.h"
using namespace std;
int main()
{
int a,b,c;
srand((int)time(0));
a=rand()%100;
if (a<50)
{
b++;
}
else
c++;
return 0;
}
第2个回答 2011-05-28
//因为你的srand每次都初始化同一个值
#include <iostream>
using namespace std;
int main()
{
srand(unsigned(time(NULL)));
int n;
int _1,_0;
while( 1 )
{
cout<<"要产生多少个随机01?"<<endl;
cin>>n;
if( n<=0 ) continue;
_1 = _0 = 0;
while( n-- )
{
if ( rand()%2 == 1 )
{
_1++;
}
else
{
_0++;
}
}
cout<<" 1 的个数: "<<_1<<endl;
cout<<" 0 的个数: "<<_0<<endl;
cout<<endl;
}
return 0;
第3个回答 2011-05-28
#include<iostream>
using namespace std;
int main()
{int a=0,b=0;
if(rand()%2==1)
a++;
else
b++;
cout<<a<<b;
return 0;
}本回答被提问者采纳