Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to randomly shuffle a vector in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>

using std::cout;
using std::endl;

int main()
{
	std::vector<int> vec(10);

	std::srand(std::time(0));

	for (int i = 0; i < 10; i++)
		vec[i] = i;

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

	for (auto i : vec) {
		cout << i << " ";
	}

	cout << endl;

	return 0;
}


/*
run:

9 4 0 2 1 8 5 3 6 7

*/

 



answered Feb 19, 2018 by avibootz
edited Feb 20, 2018 by avibootz

Related questions

1 answer 153 views
153 views asked Feb 20, 2018 by avibootz
2 answers 143 views
143 views asked Nov 8, 2023 by avibootz
1 answer 145 views
145 views asked Feb 20, 2018 by avibootz
2 answers 134 views
1 answer 89 views
89 views asked Jan 5, 2023 by avibootz
1 answer 59 views
59 views asked Nov 4, 2024 by avibootz
2 answers 146 views
146 views asked Nov 1, 2021 by avibootz
...