c++随机函数特殊的使用!

邓芳 2009-02-14 05:05:21
问个问题:我有一个数组共100个元素,第一次抽取10个随机数,第二次在剩下的90个再抽取10个随机数,第三次在剩下的80个里面再抽取10个随机数,依次类推?如何实现?
...全文
242 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
gueangyik 2010-03-11
  • 打赏
  • 举报
回复
6楼正解 而且跟楼主的意思 完全符合 没有错误
lovesi3344 2010-03-10
  • 打赏
  • 举报
回复
   /*随机生成10个0~100的数*/
#include<stdio.h>
#include<stdlib.h>
#define random(x) (rand()%x)
int main(void)
{
int x;
for(x=0; x<10; ++x)
{
printf("%d\n",random(100));
}
system("pause");
return 0;
}
#include<stdio.h>   
#include<stdlib.h>
#define random(x) (rand()%x)
int main(void)
{
int x;
for(x=0; x<10; ++x)
{
printf("%d\n",random(100));
}
system("pause");
return 0;
}
lovesi3344 2010-03-10
  • 打赏
  • 举报
回复
#include<stdio.h>   
#include<stdlib.h>
#include<time.h>
#define random(x) (rand() % x)
int main()
{
int x;
srand((int)time(0));
for(x=0;x<10;++x)
{
printf("%d\n",random(100));
}
system("pause");
return 0;
}
lovesi3344 2010-03-10
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define random(x) (rand() % x)
int main()
{
int x;
srand((int)time(0));
for(x=0;x<10;++x)
{
printf("%d\n",random(100));
}
system("pause");
return 0;
}
邓芳 2010-03-10
  • 打赏
  • 举报
回复
谢谢了,这么久没有结贴,太抱歉了!
InfidelX 2009-02-17
  • 打赏
  • 举报
回复
仔细看看STL里面的泛型算法,这个很重要
「已注销」 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 baihacker 的回复:]
C/C++ code#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

int main(void)
{

vector<int> vec;
for (int i = 0; i < 100; vec.push_back(i++));
srand(time(NULL));
random_shuffle(vec.begin(), vec.end());
for (int i = 0; i < 100; ++i)
cout << vec[i] << '\t';
return 0;
}
LZ不妨参考一下这个代码,很好的
[/Quote]
「已注销」 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 baihacker 的回复:]
C/C++ code#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

int main(void)
{

vector<int> vec;
for (int i = 0; i < 100; vec.push_back(i++));
srand(time(NULL));
random_shuffle(vec.begin(), vec.end());
for (int i = 0; i < 100; ++i)
cout << vec[i] << '\t';
return 0;
}
LZ不妨参考一下这个代码,很好的
[/Quote]
wuyanchao 2009-02-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 baihacker 的回复:]
C/C++ code#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

int main(void)
{

vector<int> vec;
for (int i = 0; i < 100; vec.push_back(i++));
srand(time(NULL));
random_shuffle(vec.begin(), vec.end());
for (int i = 0; i < 100; ++i)
cout << vec[i] << '\t';
return 0;
}
这个代码简练!!!
[/Quote]
邓芳 2009-02-17
  • 打赏
  • 举报
回复
高手!但是我不太明白原理!!
tangshuiling 2009-02-15
  • 打赏
  • 举报
回复

从符合楼主的题意方面,参考下列代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

int main(void)
{

vector<int> vec;
for (int i = 0; i < 100; vec.push_back(i++));
srand(time((unsigned int)NULL));
random_shuffle(vec.begin(),vec.end());
vector<int>::iterator iter=vec.begin();
cout<<"从"<<vec.size()<<"元素中筛选:"<<endl;
for(int i=0;i<10;++i)
{
int cnt;

for(cnt=0;cnt<10;++cnt)
{
cout<<*iter<<" ";
iter=vec.erase(iter);

}
if(!vec.empty())
cout<<endl<<"从"<<vec.size()<<"元素中筛选:";
cout<<endl;

random_shuffle(vec.begin(),vec.end());

}
return 0;
}

waizqfor 2009-02-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wpqalwaydf 的回复:]
我也是这样想的,正在编写程序!
[/Quote]
LZ random_shuffle 函数就可以了

template<class RandomAccessIterator>
void random_shuffle(
RandomAccessIterator _First,
RandomAccessIterator _Last
);
template<class RandomAccessIterator, class RandomNumberGenerator>
void random_shuffle(
RandomAccessIterator _First,
RandomAccessIterator _Last,
RandomNumberGenerator& _Rand
);

给MSDN的函数用法

// alg_random_shuffle.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>

int main( ) {
using namespace std;
vector <int> v1;
vector <int>::iterator Iter1, Iter2;

int i;
for ( i = 1 ; i <= 9 ; i++ )
v1.push_back( i );

random_shuffle( v1.begin( ), v1.end( ) );
cout << "The original version of vector v1 is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;

// Shuffled once
random_shuffle( v1.begin( ), v1.end( ));
push_heap( v1.begin( ), v1.end( ) );
cout << "Vector v1 after one shuffle is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;

// Shuffled again
random_shuffle( v1.begin( ), v1.end( ));
push_heap( v1.begin( ), v1.end( ) );
cout << "Vector v1 after another shuffle is: ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
}
邓芳 2009-02-14
  • 打赏
  • 举报
回复
我也是这样想的,正在编写程序!
baihacker 2009-02-14
  • 打赏
  • 举报
回复
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;

int main(void)
{

vector<int> vec;
for (int i = 0; i < 100; vec.push_back(i++));
srand(time(NULL));
random_shuffle(vec.begin(), vec.end());
for (int i = 0; i < 100; ++i)
cout << vec[i] << '\t';
return 0;
}
cumtxxl 2009-02-14
  • 打赏
  • 举报
回复

1、100以内随即获得0-99的一个随即值result
2、将resul项以后的元素依次前移,将resul项元素放至末尾
3、随即范围-1,获得result
4、将resul项以后的元素依次前移,将resul项元素放至倒数第二项
依次类推

1、100以内随即获得0-99的一个随即值result
2、交换数组的result项和99项的值
3、随即范围-1,获得result
4、交换result和98项的值
以此类推

baihacker 2009-02-14
  • 打赏
  • 举报
回复
random_shuffle 

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧