How to fill a list with random numbers in C++

1 Answer

0 votes
#include <iostream>
#include <list>
#include <algorithm>
#include <random>
#include <functional>

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

int main()
{
	std::list<int> lst(5);

	std::random_device rd;
	std::mt19937 mt(rd());
	std::uniform_int_distribution<int> uid(1, 100);

	auto gen = std::bind(uid, mt);

	generate(begin(lst), end(lst), gen);

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

	cout << endl;

	return 0;
}


/*
run:

95 99 98 70 63

*/

 



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

Related questions

2 answers 200 views
1 answer 200 views
1 answer 104 views
1 answer 84 views
1 answer 245 views
2 answers 230 views
1 answer 162 views
162 views asked May 14, 2018 by avibootz
...