随机数有关问题

www.MyException.Cn  网友分享于：2013-02-17  浏览：4次

1> 从三十六个数(1-36)中随机抽出七个
2>   从三十六个数(1-36)中随机抽出七个     一次取出一个.要求概率相等.

------解决方案--------------------
#include <algorithm>
#include <vector>
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int i;
vector <int> vec;
for(i=1; i <37; i++)vec.push_back(i);

vector <int> ::iterator iter1 = vec.begin();
for(; iter1 <vec.end(); iter1++)
cout < <*iter1 < < "\t ";

cout < <endl;
random_shuffle(vec.begin(), vec.end());

cout < < "you choose: " < <endl;
for(i=0; i <7; i++)
cout < <vec[i] < < "\t ";
cout < <endl;

system( "pause ");
return 0;
}
------解决方案--------------------
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main()
{
int i, tmp, array[36]={0};

srand(time(NULL));
for(i=0; i <7; i++)
{
tmp=rand()%36;
if(array[tmp] == 0)array[tmp]=1;
else i--;
}

printf( "You choose:\n ");
for(i=0; i <36; i++)
if(array[i] == 1)printf( "%d\t ", i+1);

system( "pause ");
return 0;
}
------解决方案--------------------

------解决方案--------------------