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

In this article we will discuss how to fill a std::vector with random numbers using std::generate.

For this task we will use a STL algorithm std::generate i.e.

template<typename _FIter, typename _Generator>
void  generate(_FIter start, _FIter end, _Generator gen);

It will update all the elements from range start to end -1 with values generated by gen function object. Suppose size of vector is n then std::generate will call the gen() n number of times and copy each value to vector from start to end -1 i.e (start, end].

But import point is std::generate() copies the elements i.e. it does not push elements. So, it expects that vector is already has the capacity i.e. vector’s size is already n.

Fill random Numbers in std::vector using Lambda functions

Let’s fill a std::vector of size 10 with random numbers from 0 to 100. First of all, initialize a vector with 10 ints of value 0 i.e.

// Initialize a vector with 10 ints of value 0
std::vector<int> vecOfRandomNums(10);

Now fill vector by generating 10 random numbers using lambda function,

// Generate 10 random numbers by lambda func and fill it in vector
std::generate(vecOfRandomNums.begin(), vecOfRandomNums.end(), []() {
	return rand() % 100;
});

Here, std::generate iterates the vector from begin to end. During each iteration calls the lambda function and assigns each returned value to corresponding entry in vector.

Fill Random Numbers in std::vector using a Functor

Define a Functor that will return a random numbe whenever called i.e.

struct RandomGenerator {
	int maxValue;
	RandomGenerator(int max) :
			maxValue(max) {
	}

	int operator()() {
		return rand() % maxValue;
	}
};

Now fill vector by generating 10 random numbers using above functor i.e,

// Initialize a vector with 10 ints of value 0
std::vector<int> vecOfRandomNums(10);

// Generate 10 random numbers by a Functor and fill it in vector
std::generate(vecOfRandomNums.begin(), vecOfRandomNums.end(), RandomGenerator(500));

Here, std::generate iterates the vector from begin to end. During each iteration calls the RandomGenerator functor and assigns each returned value to corresponding entry in vector.

[showads ad=inside_post]

 

Complete example is as follows,

#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>

struct RandomGenerator {
	int maxValue;
	RandomGenerator(int max) :
			maxValue(max) {
	}

	int operator()() {
		return rand() % maxValue;
	}
};
int main() {
// Initialize a vector with 10 ints of value 0
	std::vector<int> vecOfRandomNums(10);

// Generate 10 random numbers by lambda func and fill it in vector
	std::generate(vecOfRandomNums.begin(), vecOfRandomNums.end(), []() {
		return rand() % 100;
	});

	std::cout << "Random Number Generated by Lambda Function" << std::endl;
	for (int val : vecOfRandomNums)
		std::cout << val << std::endl;

// Generate 10 random numbers by a Functor and fill it in vector
	std::generate(vecOfRandomNums.begin(), vecOfRandomNums.end(),
			RandomGenerator(500));

	std::cout << "Random Number Generated by Functor" << std::endl;
	for (int val : vecOfRandomNums)
		std::cout << val << std::endl;

	return 0;
}

To compile the above code use following command

g++ –std=c++11 sample.cpp

Output:

Random Number Generated by Lambda Function
83
86
77
15
93
35
86
92
49
21
Random Number Generated by Functor
362
27
190
59
263
426
40
426
172
236

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top