注册 登录
编程论坛 C++教室

请问:怎么生成不同的随机数

商烟渺 发布于 2016-10-29 09:25, 1385 次点击
请问哪位大神有详细的随机数教程啊?还有标红的句子是什么意思啊?
int main()
{int i,j,tmp,a[100],b[100]={0};
bool flag;
srand(time(NULL));
for(i=0;i<100;++i){
    do{   
        a[i]=rand() * 100 / (RAND_MAX + 1);
    }while(b[a[i]]!=0);
    b[a[i]]=1;
}

2 回复
#2
炎天2016-10-29 10:38
while(b[a[i]]!=0); 判断为真循环继续,为假跳出循环
 b[a[i]]=1;  若a[i]=8,b[8]=1
#3
rjsp2016-10-31 09:03
程序代码:
#include <cstdlib>
#include <ctime>

#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

int main( void )
{
    int a[100];

    generate( begin(a), end(a), []{static int s=0; return s++;} );
    srand( time(nullptr) );
    random_shuffle( begin(a), end(a) );

    std::copy( begin(a), end(a), std::ostream_iterator<int>(cout," ") );
    return 0;
}
1